var MailingAddress1 = "";
var MailingAddress2 = "";
var MailingCity = "";
var MailingState = "";
var MailingZip = "";
var MailingCounty = "";
var MailingCountry = "";

function InitSaveVariables(form) {
	MailingAddress1 = form.MailingAddress1.value;
	MailingAddress2 = form.MailingAddress2.value;
	MailingCity = form.MailingCity.value;
	MailingState = form.MailingState.value;
	MailingZip = form.MailingZip.value;
	MailingCounty = form.MailingCounty.value;
	MailingCountry = form.MailingCountry.value;
}

function CopyAddress(form) {
	if (form.MailingSame.checked) {
		InitSaveVariables(form);
		form.MailingAddress1.value = form.HomeAddress1.value;
		form.MailingAddress2.value = form.HomeAddress2.value;
		form.MailingCity.value = form.HomeCity.value;
		form.MailingState.value = form.HomeState.value;       
		form.MailingZip.value = form.HomeZip.value;
		form.MailingCounty.value = form.HomeCounty.value;
		form.MailingCountry.value = form.HomeCountry.value;
	} else {
		form.MailingAddress1.value = MailingAddress1;
		form.MailingAddress2.value = MailingAddress2;
		form.MailingCity.value = MailingCity;
		form.MailingState.value = MailingState;       
		form.MailingZip.value = MailingZip;
		form.MailingCounty.value = MailingCounty;
		form.MailingCountry.value = MailingCountry;
	}
}

// disable the enter key for forms
function checkCR(evt) {
	var evt = (evt) ? evt : ((event) ? event : null);
	var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
	
	if ((evt.keyCode == 13) && (node.type=="text")) {
		return false;
	}
}

document.onkeypress = checkCR;

// preload ButtonOn Images to improve initial swap time
var aButtons = new Array();

aButtons[0] = new Image();
aButtons[0].src = "../Images/btn_please_wait_01.gif";

function preloadImages() {
	var d = document;
	if(d.images){
		if(!d.p)
			d.p = new Array();
		var i, j = d.p.length, a = preloadImages.arguments;
		for(i = 0; i < a.length; i++)
			if (a[i].indexOf("#") != 0) {
				d.p[j] = new Image;
				d.p[j++].src = a[i];
			}
	}
}

function swapImgRestore() {
	var i, x, a = document.sr;
	for(i = 0; a && i < a.length && (x =a [i]) && x.oSrc; i++)
		x.src = x.oSrc;
}

function findObj(n, d) {
	var p, i, x;
	if(!d)
		d = document;
	if((p = n.indexOf("?")) > 0 && parent.frames.length) {
		d = parent.frames[n.substring(p + 1)].document;
		n = n.substring(0,p);
	}
	if(!(x = d[n]) && d.all)
		x = d.all[n];
	for(i = 0; !x && i < d.forms.length; i++)
		x = d.forms[i][n];
	for(i = 0; !x && d.layers && i < d.layers.length;i++)
		x = findObj(n, d.layers[i].document);
	if(!x && d.getElementById)
		x = d.getElementById(n);
	return x;
}

function swapImage() {
	var i, j = 0, x, a = swapImage.arguments;
	document.sr = new Array;
	for(i = 0; i < (a.length - 2); i+=3)
		if ((x = findObj(a[i])) != null) {
			document.sr[j++] = x;
			if(!x.oSrc)
				x.oSrc = x.src;
				x.src = a[i+2];
		}
}

var cnt = 0;

function disablesubmit(theform) {
	if (cnt == 0) {
		theform.Submit.disabled = true;
		//theform.Submit.value = "Please Wait...";
		cnt++;
		return(true);
	} else {
		alert("You have already submitted the form.\nClick 'OK' and wait for the form\nto finish processing.");
	}
}

function PopupPic(sPicURL) {
	var winProp = 'width=200,height=200,left=200,top=200,scrollbars=0,resizable=1,menubar=yes,status=0';
	window.open( "popup.asp?"+sPicURL, "", winProp);
}

function PopupWin(sWinURL) {
	var sWinProp = 'width=800,height=600,left=200,top=200,toolbar=1,scrollbars=1,location=0,status=1,menubar=1,resizable=1';
	window.open(sWinURL, "", sWinProp);
}

function Popup(sURL,sName,sWidth,sHeight) {
	var winProp = 'width=' + sWidth + ',height=' + sHeight + ',left=200,top=200,scrollbars=0,resizable=1,menubar=no';
	window.open(sURL, sName, winProp);
}

function formatphone(x){
	var vphone = /^\D*([1-9]\d{2})\D*(\d{3})\D*(\d{4})\D*$/

	if (x.value != 0){
		if(vphone.test(x.value)){
			x.value=x.value.replace(vphone,'($1) $2-$3');
		}
		else {
			alert('Invalid phone number!\n\nSample: (555) 555-5555');
			x.focus();
			x.select();
		}
	}
}

function disableForm(theform) {
	if (document.all || document.getElementById) {
		for (i = 0; i < theform.length; i++) {
			var tempobj = theform.elements[i];
		if (tempobj.type.toLowerCase() == "submit")
			tempobj.disabled = true;
			document.form.Submit.value = "Wait...";
		}
		return true;
	}
}

function placeFocus() {
if (document.forms.length > 0) {
	var field = document.forms[0];
	for (i = 0; i < field.length; i++) {
		if ((field.elements[i].type == "text") || (field.elements[i].type == "textarea") && (field.elements[i].disabled != true)) {
			if (field.elements[i].disabled != true) {
				document.forms[0].elements[i].focus();
				break;
				}
         }
      }
   }
}

// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

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++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function CheckSSN(field) { 
	var SSN = field; 
	var nums = SSN.value.replace(/[^0-9]/ig, ''); 
	if (!nums) {
		return;
	}
	if (nums.length == 9) {
		SSN.value = nums.substring(0, 3) + '-' +
		nums.substring(3, 5) + '-' +
		nums.substring(5, 9);
	} else {
		alert("Invalid SSN Format!\n\nPlease format the SSN using XXX-XX-XXXX.");
		SSN.select();
		SSN.focus();
	}
}

function ValidateDate2(field){
	var checkstr = "0123456789";
	var month = "";
	var Datevalue = "";
	var i;
	var err = 0;
	err = 0;
	month = field.value

	if (month != "") {
		for (i = 0; i < month.length; i++) {
			if (checkstr.indexOf(month.substr(i,1)) < 0) {
				err = 1;
			}
		}

		if (month.length == 1) {
			month = '0' + month;
		}

		if (month.length != 2) {
			err = 1;
		}

		if (err == 0) {
			field.value = month;
		}

		if (err != 0) {
			alert("Invalid Date Format!\n\nPlease format the date using mm.");
			field.select();
			field.focus();
		}
	}
}

function ValidateDate3(field){
	var checkstr = "0123456789";
	var year = "";
	var Datevalue = "";
	var i;
	var err = 0;
	err = 0;
	year = field.value

	if (year != "") {
		for (i = 0; i < year.length; i++) {
			if (checkstr.indexOf(year.substr(i,1)) < 0) {
				err = 1;
			}
		}

		if (year.length == 2) {
			year = '20' + year;
		}

		if (year.length != 4) {
			err = 1;
		}

		if (err == 0) {
			field.value = year;
		}

		if (err != 0) {
			alert("Invalid Date Format!\n\nPlease format the date using yyyy.");
			field.select();
			field.focus();
		}
	}
}

function PopupSmall(myurl) {
	var newWindow;
	var props = 'scrollBars=no,resizable=yes,toolbar=no,menubar=no,location=no,directories=no,width=380,height=300,left=200,top=200';
	newWindow = window.open(myurl, "PopupSmall", props);
}

// Adds the list of selected items selected in the child
// window to its list. It is called by child window to do so.  
function addToParentList(AddressType,Name,Address1,Address2,City,State,Zip,Country) {
	destList = window.document.forms["NPAReportForm"].SitesOfProgram;

	var count = destList.options.length;
	var sName = xReplace(Name.value,',','');
	var sAddress1 = xReplace(Address1.value,',','');
	var sAddress2;
	var sCity = xReplace(City.value,',','');

	if (Address2.value != '') {
		sAddress2 =  xReplace(Address2.value,',','');
	} else {
		sAddress2 = '';
	}

	if (Name.value != '' && Address1.value != '' && City.value != '' && State.value != '' && Zip.value != '') {
		destList.options[count] = new Option(AddressType.value + ': ' + sName + ' ...',AddressType.value + '|' + sName + '|' + sAddress1 + '|' + sAddress2 + '|' + sCity + '|' + State.value + '|' + Zip.value + '|' + Country.value);
	}
}

// Marks all the items as selected for the submit button.  
function selectList() {
	for(i = 0; i < window.document.forms["NPAReportForm"].SitesOfProgram.length; i++) {
		if (window.document.forms["NPAReportForm"].SitesOfProgram.options[i] != null)
			window.document.forms["NPAReportForm"].SitesOfProgram.options[i].selected = true;
	}
	return true;
}

// Deletes the selected items of supplied list.
function deleteSelectedItemsFromList(sourceList) {
	var maxCnt = sourceList.options.length;
	for(var i = maxCnt - 1; i >= 0; i--) {
		if ((sourceList.options[i] != null) && (sourceList.options[i].selected == true)) {
			sourceList.options[i] = null;
		}
	}
}

// Add the selected items in the parent by calling method of parent
function addSelectedItemsToParent() {
	self.opener.addToParentList(window.document.forms[0].AddressType,window.document.forms[0].Name,window.document.forms[0].Address1,window.document.forms[0].Address2,window.document.forms[0].City,window.document.forms[0].State,window.document.forms[0].Zip,window.document.forms[0].Country);
	window.close();
}

function xReplace(checkMe,toberep,repwith){
	temp = checkMe;
	a = 0;

	for(i = 0; i < checkMe.length; i++){
		a = temp.indexOf(toberep);
		temp = temp.substring(0 , a) + repwith + temp.substring((a + toberep.length));

		if (a == -1){
			break;
		}
	}
	return temp;
}

function isDate(dtStr, dtField){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be mm/dd/yyyy for the " + dtField + " field")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month for the " + dtField + " field")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day for the " + dtField + " field")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between " + minYear + " and " + maxYear + " for the " + dtField + " field")
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date for the " + dtField + " field")
		return false
	}
return true
}