var bSubmitted = false;

// onUnload Exit Pop Functionality to Display Additional Offers
function onul(cURL,iType,iExit) {
    if(!bSubmitted && iType!=3 && iType!=4 && iExit!=1) {
        window.open(cURL);
        self.focus();
    }
}

function CheckEmpty(boxName, Readable){
	if (boxName.value == ""){
	    alert("You must enter your " + Readable + ".")
	    boxName.focus()
	    return false
	}else{
		return true
	}
}

function CheckName(boxName, Readable){
	if (boxName.value.length < 2){
	    alert("Please enter your full " + Readable + ".")
    	boxName.focus()
	    return false
	}else{
		return true
	}
}

function CheckIndex(boxName, Readable){
	if(boxName.selectedIndex < 1){
		alert('Please select one.');
		boxName.focus();
		return false;
	}else{
		return true
	}
}


function CheckMail(email){
	var checkTLD=0;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=email.match(emailPat);
	if (matchArray==null) {
		alert("Email address seems incorrect (check @ and .'s)");
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];

	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			alert("Ths email address contains invalid characters.");
			return false;
		}
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			alert("Ths email address contains invalid characters.");
			return false;
		}
	}
	if (user.match(userPat)==null) {
		alert("The email address doesn't seem to be valid.");
		return false;
	}
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				alert("Destination IP address is invalid!");
				return false;
			}
		}
		return true;
	}
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			alert("The email address does not seem to be valid.");
			return false;
		}
	}
	if (checkTLD && domArr[domArr.length-1].length!=2 &&
	domArr[domArr.length-1].search(knownDomsPat)==-1) {
		alert("The address must end in a well-known domain or two letter " + "country.");
		return false;
	}
	if (len<2) {
		alert("This email address is missing a hostname!");
		return false;
	}
	return true;
}

function ValidPhone(boxName, num){
	var o = document.reg;
	valid = "0123456789";
	for(i = 0; i < boxName.value.length; i++){
		if(valid.indexOf(boxName.value.charAt(i))==-1){
			alert('Please Enter a Valid Phone Number');
			boxName.focus();
			return false
		}
	}
	if(boxName == o.area_code){
		if (boxName.value.charAt(0)=='0'){
			alert('Invalid Area Code');
			boxName.focus();
			return false
		}
		if (boxName.value.charAt(0)=='1'){
			alert('Invalid Area Code');
			boxName.focus();
			return false
		}
	}
	if(boxName == o.homephone1){
		if (boxName.value.charAt(0)=='0'){
			alert('Please Enter a Valid Phone Number');
			boxName.focus();
			return false
		}
	}
	if (boxName.value.length<num){
		alert('Please Enter a Valid Phone Number');
		boxName.focus();
		return false
	}
	return true;
}

function format_name(boxName){
	len=boxName.value.length;
	first_letter=boxName.value.substr(0,1);
	second_letters = boxName.value.substr(1,len);
	boxName.value = first_letter.toUpperCase()+second_letters.toLowerCase();
}

function make_text(text){
	len=text.length;
	first_letter=text.substr(0,1);
	second_letters = text.substr(1,len);
	retval = first_letter.toUpperCase()+second_letters.toLowerCase();
	return retval
}

function format_text(boxName){
	len=boxName.value.length;
	text_before = boxName.value.toLowerCase();
	start=0;
	final="";
	while(text_before.indexOf(" ")!=-1){
		pos = text_before.indexOf(" ");
		final=final+make_text(text_before.substr(0,pos))+" ";
		len=text_before.length;
		text_before = text_before.substr(pos,len);

		pos=0;
		while((text_before.charAt(pos)==" ") && (pos!=len)){
			pos = pos+1;
		}
		text_before = text_before.substr(pos,len);
		tmp = text_before.indexOf(" ");
	}
	final = final+make_text(text_before);
	boxName.value = final;

}
function format_email(boxName){
	len=boxName.value.length;
	email = boxName.value.substr(0,len);
	boxName.value = email.toLowerCase();
}

function format_zip(boxName){
	len=boxName.value.length;
	zip = boxName.value.substr(0,len);
	boxName.value = zip.toUpperCase();
}

function numbers_in_name(boxName){
	valid = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz-' ";

	for(i = 0; i < boxName.value.length; i++){
		if(valid.indexOf(boxName.value.charAt(i))==-1){
			alert('Only Characters allowed in name');
			boxName.focus();
			return false
		}

	}
	return true
}

function str_replace(boxName){
	var i = boxName.value.indexOf(",");
	while(i != -1){
		boxName.value = boxName.value.replace(","," ");
		i = boxName.value.indexOf(",");
	}

	var j = boxName.value.indexOf("'");
	while(j != -1){
		boxName.value = boxName.value.replace("'"," ");
		j = boxName.value.indexOf("'");
	}
	return(boxName.value);
}


function make_phone(){
	var o = document.reg;
	var tmp_phone = o.homephone1.value+o.homephone2.value;
	var phone=o.area_code.value+o.homephone1.value+o.homephone2.value;
	var curr;
	var index_char = phone.charAt(0);
	var count=0;
	for(i = 0; i < phone.length; i++){
		if(count<5){
			curr = phone.charAt(i);

			if (curr == index_char){
				count = count+1;
			}else{
				index_char = curr;
				count=1;
			}
		}
	}
	if (count>=5){
    	phone = "";
    }
	if(tmp_phone == "5551212"){
    	phone="";
    }
	o.phone.value=phone;
}


function checkit(pageid){

	var o = document.reg;

	if(pageid == 1){
    	if (CheckEmpty(o.iAge, "Age") &&
        	CheckEmpty(o.iGender, "Gender") &&
        	CheckEmpty(o.countryid, "Country") &&
        	CheckEmpty(o.reason, "Reason for wanting to start your own business.")){

    		return true;
    	}else{
    		return false;
    	}
	}else if(pageid == 2){
    	if (CheckEmpty(o.started, "Desired Monthly Income") &&
        	CheckEmpty(o.important, "How soon you could start") &&
        	CheckEmpty(o.creditrate, "Personal credit rating")){

    		return true;
    	}else{
    		return false;
    	}
	}else if(pageid == 3){
    	if (CheckEmpty(o.employed, "How soon you could start") &&
    	    CheckEmpty(o.salary, "Salary to which you are accustomed") &&
        	CheckEmpty(o.money, "Time to invest") &&
        	CheckEmpty(o.invest, "amount to invest")){

    		return true;
    	}else{
    		return false;
    	}
	}else if(pageid == 4){
    	make_phone();
    	o.firstname.value = str_replace(o.firstname);
    	o.lastname.value = str_replace(o.lastname);
    	o.address.value = str_replace(o.address);
    	o.city.value = str_replace(o.city);
    	o.zipcode.value = str_replace(o.zipcode);

    	if (CheckEmpty(o.firstname, "First Name") &&
    	    numbers_in_name(o.firstname) &&
        	CheckEmpty(o.lastname, "Last Name") &&
        	numbers_in_name(o.lastname) &&
        	CheckName(o.firstname, "First Name") &&
        	CheckName(o.lastname, "Last Name") &&
        	CheckEmpty(o.address, "Address") &&
        	CheckEmpty(o.city, "City") &&
        	CheckEmpty(o.state, "State") &&
        	CheckEmpty(o.zipcode, "Zip Code") &&
        	CheckEmpty(o.area_code, "Area Code") &&
        	CheckEmpty(o.homephone1, "Phone Number") &&
        	CheckEmpty(o.homephone2, "Phone Number") &&
        	ValidPhone(o.area_code,3) &&
        	ValidPhone(o.homephone1,3) &&
        	ValidPhone(o.homephone2,4) &&
        	CheckMail(o.email.value, "E-mail") && (!bSubmitted)){

    		format_text(o.firstname);
    		format_text(o.lastname);
    		format_text(o.city);
    		format_text(o.address);
    		format_email(o.email);
    		format_zip(o.zipcode);
        	if(!o.termsok.checked){
    			alert('You must agree to the terms of our Privacy Policy agreement before we can continue.');
        		bSubmitted = false;
        		return false;
        	}else{
        		bSubmitted = true;
        		return true;
        	}
    	}else{
    		return false;
    	}
    }else{
		return false;
    }
}


function autotab(original,destination){
	if (original.getAttribute&&original.value.length==original.getAttribute("maxlength"))
	destination.focus()
}

