/*
 ValidateMain(form_object)
 USAGE:
 <input type="hidden" name="_^[A][--+B]" value="[C]"/>

 [A]: A valid javascript condition where 'f' is predefined to be the field object
 preceeding the hidden condition field, ge('name or id') returns given field object,
 and rc('name or id') returns string of 1s and 0s representing the checked or
 unchecked state of a group of fields with the given name or id
 
 [--+B]: Optional.  Defines the field name or id that should receive the focus
 after the pop up message.  By default, the field preceeding the hidden
 condition field receives the focus, however this may not be desirable
 
 [C]: Pop up message
 
 ValidateShort(form_object) and BufferFields()
 USAGE:
 Use this pair of functions to eliminate default text in a field upon submission.
 ValidateShort replaces ValidateMain for this purpose. It additionally 
 predefines 'iv' as the initial value of the field preceeding the hidden condition
 field for use in the condition 
 
 NOTE: iv is defined only for the field preceeding the hidden condition field,
 therefore, consecutive condition fields aren't supported for use with this var
 
 Buffer fields should be placed on the page after all forms that use ValidateShort
 
*/

var arrFormFields, iOffset=0;

//Buffer up to one hundred fields from forms on page
var arrFormFields=Array(100);
for(i=0;i<100;i++)
	arrFormFields[i]=Array(2);

//Validation code-------------------------------------------
function ValidateMain(frm){  
	var i, k=0, f, iv, focus_value, value_pair, condition_value;
	e=frm.elements;

    
    //saman---
	var contLoop
	var nextTagIndex
	nextTagIndex = 0
	//-----------
	
	
	for(i=0;i<e.length;i++){
		iv=getFieldByIndex(frm, i-1);

        //saman--
		contLoop = true;
		//--------


		if (e[i].name != null) {


		    //saman---------
		    if (e[i].id == "intakeFormIPhoneNumber") {
		        
		        if (ValidatePhoneNumber(e[i]) == false) {
		            e[i].focus();

		            return false;
		        }
		        
		        contLoop = false;
		        nextTagIndex = i++;

		    }

		    if (i == nextTagIndex) {
		        contLoop = false;
		    }
		    //----------------

		    if (contLoop) {

		        if (e[i].name.substring(0, 2) == '_^') {
		            f = e[k];

		            value_pair = e[i].name.split('--+');

		            focus_value = '';
		            if (value_pair.length > 1) {
		                focus_value = value_pair[1];
		                condition_value = value_pair[0].substring(2, value_pair[0].length);
		            }
		            else
		                condition_value = e[i].name.substring(2, e[i].name.length);

		            //alert(condition_value);

		            if (eval(condition_value)) {
		                alert(e[i].value);

		                if (focus_value != '') {
		                    focus_value = ge(focus_value);
		                    focus_value.focus();
		                    if (focus_value.type != 'select-one')
		                        focus_value.select();
		                }
		                else {
		                    e[k].focus();
		                    if (e[k].type != 'select-one')
		                        e[k].select();
		                }

		                return false;
		            }
		        }
		        else
		            k = i;
		    }
		} 
	}
	
	return true;
}
 
function ge(sIdName){
 var j=0;
 for(j=0;j<e.length;j++)
  if(e[j].id==sIdName||e[j].name==sIdName)
   return e[j];
 
 alert('ge: No name or id "'+sIdName+'"');
 return e[0]; 
}
 
function rc(sIdName){
 var j=0,sBString='';
 for(j=0;j<e.length;j++)
  if(e[j].id==sIdName||e[j].name==sIdName)
   sBString+=(e[j].checked)?'1':'0';   
  
  if(sBString=='')
   alert('rc: No name or id "'+sIdName+'"');
  return sBString;
  
}
//Validation code-----------------------------------	



//Buffer form index, field name, and field value into arrFormFields for all forms in document
function bufferFields(){
	var i,j;
	for(i=0;i<document.forms.length;i++){			
		for(j=0;j<document.forms[i].elements.length;j++){									
			arrFormFields[iOffset][0]=i;
			arrFormFields[iOffset][1]=document.forms[i].elements[j].name;
			arrFormFields[iOffset][2]='';
			
			if(document.forms[i].elements[j].type=='text' || document.forms[i].elements[j].type=='textarea'){
				arrFormFields[iOffset][2]=document.forms[i].elements[j].value;				
			}	
	
			//alert(arrFormFields[iOffset][0] + ', ' + arrFormFields[iOffset][1] + ', ' + arrFormFields[iOffset][2] + ', ' + iOffset);
			iOffset++;
		}		
	}
	return 0;
}
//Get field value from form object and field name in the arrFormFields array
function getFieldByName(frm, fieldName){
	var i,formIndex = getFormIndex(frm);
	
	for(i=0;i<iOffset;i++)
		if(arrFormFields[i][0]==formIndex&&arrFormFields[i][1]==fieldName){
			return arrFormFields[i][2];
		}
			
	return 0;
}
//Get field value from form object and element index in the arrFormFields array
function getFieldByIndex(frm, index){
	var i,j=-1,formIndex = getFormIndex(frm);

	for(i=0;i<iOffset;i++){
		if(arrFormFields[i][0]==formIndex&&j==-1)		
			j=i;
		
		if(i-j==index&&j!=-1)
			return arrFormFields[i][2];
	}
			
	return 0;
}

//Set value of all fileds that still have default values to null string in a given form
function clearDefaultValues(frm){
	var i;
	for(i=0;i<frm.elements.length;i++)
		if(frm.elements[i].value==getFieldByIndex(frm,i))
			frm.elements[i].value='';

	return true;
}

//Use this to validate short forms and clear default values
function ValidateShort(frm){
	
	if(ValidateMain(frm))
		return clearDefaultValues(frm);
	
	return false;

}

//Get form index for given form
function getFormIndex(frm){
	var i;
	for(i=0;i<document.forms.length;i++)
		if(document.forms[i]==frm)
			return i;
	
	return 0;
}



//========================= PHONE VALIDATION



/**
* DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
*/

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;
// Maximum no of digits in an america phone no.
var maxDigitsInIPhoneNumber = 13;
//US Area Code
var AreaCode = new Array(205, 251, 659, 256, 334, 907, 403, 780, 264, 268, 520, 928, 480, 602, 623, 501, 479, 870, 242, 246, 441, 250, 604, 778, 284, 341, 442, 628, 657, 669, 747, 752, 764, 951, 209, 559, 408, 831, 510, 213, 310, 424, 323, 562, 707, 369, 627, 530, 714, 949, 626, 909, 916, 760, 619, 858, 935, 818, 415, 925, 661, 805, 650, 600, 809, 345, 670, 211, 720, 970, 303, 719, 203, 475, 860, 959, 302, 411, 202, 767, 911, 239, 386, 689, 754, 941, 954, 561, 407, 727, 352, 904, 850, 786, 863, 305, 321, 813, 470, 478, 770, 678, 404, 706, 912, 229, 710, 473, 671, 808, 208, 312, 773, 630, 847, 708, 815, 224, 331, 464, 872, 217, 618, 309, 260, 317, 219, 765, 812, 563, 641, 515, 319, 712, 876, 620, 785, 913, 316, 270, 859, 606, 502, 225, 337, 985, 504, 318, 318, 204, 227, 240, 443, 667, 410, 301, 339, 351, 774, 781, 857, 978, 508, 617, 413, 231, 269, 989, 734, 517, 313, 810, 248, 278, 586, 679, 947, 906, 616, 320, 612, 763, 952, 218, 507, 651, 228, 601, 557, 573, 636, 660, 975, 314, 816, 417, 664, 406, 402, 308, 775, 702, 506, 603, 551, 848, 862, 732, 908, 201, 973, 609, 856, 505, 575, 585, 845, 917, 516, 212, 646, 315, 518, 347, 718, 607, 914, 631, 716, 709, 252, 336, 828, 910, 980, 984, 919, 704, 701, 283, 380, 567, 216, 614, 937, 330, 234, 440, 419, 740, 513, 580, 918, 405, 905, 289, 647, 705, 807, 613, 519, 416, 503, 541, 971, 445, 610, 835, 878, 484, 717, 570, 412, 215, 267, 814, 724, 902, 787, 939, 438, 450, 819, 418, 514, 401, 306, 803, 843, 864, 605, 869, 758, 784, 731, 865, 931, 423, 615, 901, 325, 361, 430, 432, 469, 682, 737, 979, 214, 972, 254, 940, 713, 281, 832, 956, 817, 806, 903, 210, 830, 409, 936, 512, 915, 868, 649, 340, 385, 435, 801, 802, 276, 434, 540, 571, 757, 703, 804, 509, 206, 425, 253, 360, 564, 304, 262, 920, 414, 715, 608, 307, 867)

function isInteger(s) {
    var i;
    for (i = 0; i < s.length; i++) {
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag) {
    var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++) {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}
function trim(s) {
    var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++) {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
function checkInternationalPhone(strPhone) {
    strPhone = trim(strPhone)
    if (strPhone.indexOf("00") == 0) strPhone = strPhone.substring(2)
    if (strPhone.indexOf("+") > 1) return false
    if (strPhone.indexOf("+") == 0) strPhone = strPhone.substring(1)
    if (strPhone.indexOf("(") == -1 && strPhone.indexOf(")") != -1) return false
    if (strPhone.indexOf("(") != -1 && strPhone.indexOf(")") == -1) return false
    s = stripCharsInBag(strPhone, validWorldPhoneChars);
    if (strPhone.length > 10) { var CCode = s.substring(0, s.length - 10); }
    else { CCode = ""; }
    if (strPhone.length > 7) { var NPA = s.substring(s.length - 10, s.length - 7); }
    else { NPA = "" }
    var NEC = s.substring(s.length - 7, s.length - 4)
    if (CCode != "" && CCode != null) {
        if (CCode != "1" && CCode != "011" && CCode != "001") return false
    }
    if (NPA != "") {
        if (checkAreaCode(NPA) == false) { //Checking area code is vaid or not
            return false
        }
    }
    else { return false }
    return (isInteger(s) && s.length >= minDigitsInIPhoneNumber && s.length <= maxDigitsInIPhoneNumber);
}
//Checking area code is vaid or not
function checkAreaCode(val) {
    var res = false;
    for (var i = 0; i < AreaCode.length; i++) {
        if (AreaCode[i] == val) res = true;
    }
    return res
}







function ValidatePhoneNumber(inputPhone) {


    if ((inputPhone.value != null) && (inputPhone.value != "") && (inputPhone.value != "Phone")) {


        if (checkInternationalPhone(inputPhone.value) == false) {
            alert("Please Enter a Valid Phone Number")

            inputPhone.focus()
            
            return false
        }
    }

        return true;
}
