function getprofile(sesid,proid){
	if(document.getElementById('profile')){
		document.getElementById('profile').innerHTML="<iframe src='.\\hplib\\profile.cgi?mode=getaddr&sec=" + sesid + "&id=" + proid + "' height=0 width=0 scrollbars=no frameborder=0></iframe>";
	}
}
function getEyeProfile(sesid,proid){
	document.getElementById('profile').innerHTML="<iframe src='.\\hplib\\profile.cgi?id=" + proid + "' height=0 width=0 scrollbars=no frameborder=0></iframe>";
	//checkchange();
}
function prodelete(sesid,proid){
	document.getElementById('ddelete').innerHTML="<iframe src='.\\hplib\\profile.cgi?mode=del&sec=" + sesid + "&id=" + proid + "' height=0 width=0 scrollbars=no frameborder=0></iframe>";
}

function encode(str) {
	var strOut = escape(str);
	
	var result = "";
	
	for (i = 0; i < strOut.length; i++) {
		if (strOut.charAt(i) == "+"){
			result += '%2B';
		}else if(strOut.charAt(i) == " "){
			result += "+";
		}else{
			result += strOut.charAt(i);
		}
	}
	
	return result;
}

function checkform2(sesid){
	
	if (document.myForm2.wearername.value == ''){
		alert('Please Enter Name');
		return 0;}
		
		if (document.myForm2.ODCylinder[document.myForm2.ODCylinder.selectedIndex].value!='-'&&document.myForm2.ODAxis[document.myForm2.ODAxis.selectedIndex].value=='-'){
		if (document.myForm2.ODCylinder[document.myForm2.ODCylinder.selectedIndex].value!='D.S.'&&document.myForm2.ODCylinder[document.myForm2.ODCylinder.selectedIndex].value!='SPH'){
		alert('Please enter OD Axis');
		return 0;
		}}
		
		if (document.myForm2.OSCylinder[document.myForm2.OSCylinder.selectedIndex].value!='-'&&document.myForm2.OSAxis[document.myForm2.OSAxis.selectedIndex].value=='-')
		if (document.myForm2.OSCylinder[document.myForm2.OSCylinder.selectedIndex].value!='D.S.'&&document.myForm2.OSCylinder[document.myForm2.OSCylinder.selectedIndex].value!='SPH'){
		alert('Please enter OS Axis');
		return 0;
	}

if (document.myForm2.PupillaryDistance.value == ''){
alert('Please enter PD');
return 0;}

//CHECK PD Range 50~70
var PDValue = parseInt(document.myForm2.PupillaryDistance.value);
if(!(PDValue >= 50 && PDValue <= 70)){
	if(!confirm('The P.D. (Pupillary Distance) you entered is out of range of a normal size person. \nThis box comes out just to make sure you\'re making correct P.D. input\n\nYes - Continue\nNo  - Re enter')){
		return 0;
	}
}

//new ODOS warning parseInt
//Checking Ossphere +-
var warning = false;
if (document.myForm2.ODSphere.value && document.myForm2.OSSphere.value){
	if(parseFloat(document.myForm2.ODSphere.value) > 0){
		if(parseFloat(document.myForm2.OSSphere.value) < 0){
			warning = true;
		}
	}else{
		if(parseFloat(document.myForm2.OSSphere.value) > 0){
			warning = true;
		}
	}
	if(warning){
		if(!confirm('The Sphere values looks like abnormal . (One is positive and the other is negative) \nPlease double check your sphere values to see if there are any mistakes\n\nYes - Continue\nNo  - Re enter')){
			return 0;
		}
	}
}
//checking cylinder
warning = false;
if (document.myForm2.ODCylinder.value && document.myForm2.OSCylinder.value){
	if(parseFloat(document.myForm2.ODCylinder.value) > 0){
		if(parseFloat(document.myForm2.OSCylinder.value) < 0){
			warning = true;
		}
	}else{
		if(parseFloat(document.myForm2.OSCylinder.value) > 0){
			warning = true;
		}
	}
	if(warning){
		if(!confirm('Your cylinder values looks like incorrect . (One is positive and the other is negative) \nPlease double check your inputs .\n\nYes - Continue\nNo  - Re enter the cylinder value')){
			return 0;
		}
	}
}
//checking axis
warning = false;
if (document.myForm2.ODAxis.value && document.myForm2.OSAxis.value){
	var ODAxisValue = parseFloat(document.myForm2.ODAxis.value);
	var OSAxisValue = parseFloat(document.myForm2.OSAxis.value);
	if(Math.abs(ODAxisValue - OSAxisValue) > 70 && Math.abs(ODAxisValue - OSAxisValue) < 110){
		warning = true;
	}
	if(warning){
		if(!confirm('Your axis value looks like abnormal. \nThere\'s only 3-4% people in the world have the Axis value like this. \nNeed a double check ?\n\nYes - Continue to order\nNo  - Re enter your axis')){
			return 0;
		}
	}
}
//Checking Add
warning = false;
if (document.myForm2.ODAdd.value != document.myForm2.OSAdd.value){
	warning = true;
}
if(warning){
	if(!confirm('Add values of left and right sides are not the same. What about a double check ?\n\nYes - Continue\nNo  - Re enter')){
		return 0;
	}
}
save(sesid);
}
function save(sesid){
	var query = '.\\hplib\\profile.cgi?mode=save&sec=' + sesid;
	query += '&wearername=' + encode(document.myForm2.wearername.value);
	query += '&wearerage=' + encode(document.myForm2.wearerage.value);
	query += '&wearerheadsize=' + encode(document.myForm2.wearerheadsize.value);
	query += '&lenswidth=' + encode(document.myForm2.lenswidth.value);
	query += '&lensheight=' + encode(document.myForm2.lensheight.value);
	query += '&templelength=' + encode(document.myForm2.templelength.value);
	query += '&framewidth=' + encode(document.myForm2.framewidth.value);
	query += '&prescriptionyr=' + encode(document.myForm2.prescriptionyr.value);
	query += '&pupillarydistance=' + encode(document.myForm2.pupillarydistance.value);
	//if(document.myForm2.framesize[0].checked){
	//	query += '&framesize=' + encode(document.myForm2.framesize[0].value);
	//}else if(document.myForm2.framesize[1].checked){
		query += '&framesize=' + encode('current');
	//}
	query += '&ODSphere=' + encode(document.myForm2.ODSphere.value);
	query += '&ODCylinder=' + encode(document.myForm2.ODCylinder.value);
	query += '&ODAxis=' + encode(document.myForm2.ODAxis.value);
	query += '&ODprism=' + encode(document.myForm2.ODprism.value);
	query += '&ODprismpos=' + encode(document.myForm2.ODprismpos.value);
	query += '&ODAdd=' + encode(document.myForm2.ODAdd.value);
	query += '&OSSphere=' + encode(document.myForm2.OSSphere.value);
	query += '&OSCylinder=' + encode(document.myForm2.OSCylinder.value);
	query += '&OSAxis=' + encode(document.myForm2.OSAxis.value);
	query += '&OSprism=' + encode(document.myForm2.OSprism.value);
	query += '&OSprismpos=' + encode(document.myForm2.OSprismpos.value);
	query += '&OSAdd=' + encode(document.myForm2.OSAdd.value);
	query += '&profile=' + encode(document.myForm2.profile.value);
	document.getElementById('dsave').innerHTML="<iframe src='"+ query +"' height=0 width=0 scrollbars=no frameborder=0></iframe>";
}

function checkchange(){
}

function getaddrprofile(sesid,proid){
	document.getElementById('profile').innerHTML="<iframe src='.\\hplib\\profile.cgi?mode=getaddr&sec=" + sesid + "&id=" + proid + "' height=0 width=0 scrollbars=no frameborder=0></iframe>";
}

function proaddrdelete(sesid,proid){
	document.getElementById('ddelete').innerHTML="<iframe src='.\\hplib\\profile.cgi?mode=deladdr&sec=" + sesid + "&id=" + proid + "' height=0 width=0 scrollbars=no frameborder=0></iframe>";
}

function saveaddr(sesid){
		var query = '.\\hplib\\profile.cgi?mode=saveaddr&sec=' + sesid;
		query += '&sex=' + encode(document.address.sex.value);
		query += '&firstname=' + encode(document.address.firstname.value);
		query += '&lastname=' + encode(document.address.lastname.value);
		query += '&email=' + encode(document.address.email.value);
		query += '&street1=' + encode(document.address.street1.value);
		query += '&street2=' + encode(document.address.street2.value);
		query += '&city=' + encode(document.address.city.value);
		query += '&state=' + encode(document.address.state.value);
		query += '&zip=' + encode(document.address.zip.value);
		query += '&country=' + encode(document.address.country.value);
		query += '&phone=' + encode(document.address.phone.value);
		
		for(i = 0 ; i < document.address.paymethod.length ; i++){
			if(document.address.paymethod[i].checked){
				query += '&paymethod=' + encode(document.address.paymethod[i].value);
				break;
			}	
		}
		/*
		if(document.address.paymethod[0].checked){
			query += '&paymethod=' + encode(document.address.paymethod[0].value);
		}else if(document.address.paymethod[1].checked){
			query += '&paymethod=' + encode(document.address.paymethod[1].value);
		}
		*/
		query += '&profile=' + encode(document.address.profile.value);
		document.getElementById('dsave').innerHTML="<iframe src='" + query + "' height=0 width=0 scrollbars=no frameborder=0></iframe>";			
}
function showError(objInput){
	objInput.style.backgroundColor = "#FFFF99";	
}
function showNormal(objInput){
	objInput.style.backgroundColor = "#EAEFF2";
}
function checksubmit(sesid){
	showNormal(document.address.phone);
	showNormal(document.address.sex);
	showNormal(document.address.firstname);
	showNormal(document.address.lastname);
	showNormal(document.address.street1);
	showNormal(document.address.zip);
	showNormal(document.address.country);
	showNormal(document.address.email);
	
	var lblError = document.getElementById('lblError');
	
	lblError.style.backgroundColor = "#FFBBBB";
	lblError.style.border = "1px solid #FF0000";

	lblError.innerHTML = "<h1 style='font-size:14px'>Error :</h1>";
	var intPass = 1;
	
	//20070704 Require Phone When first priority
	if(document.address.express != null){
		if(document.address.express.checked){
			if(document.address.phone.value == ''){
				lblError.innerHTML += "<b>&nbsp;Phone Number Required on First Priority Services.</b><br/><br/>"
				showError(document.address.phone);
				intPass = 0;
			}
		}
	}
	if(document.address.sex.value == ''){
		lblError.innerHTML += "<b>&nbsp;Mr or Miss is required</b><br/><br/>";
		showError(document.address.sex);
		intPass = 0;
	}
	if(document.address.firstname.value == ''){
		lblError.innerHTML += "<b>&nbsp;First Name is required.</b><br/><br/>";
		showError(document.address.firstname);
		intPass = 0;
	}
	if(document.address.lastname.value == ''){
		lblError.innerHTML += "<b>&nbsp;Last Name is required.</b><br/><br/>";
		showError(document.address.lastname);
		intPass = 0;
	}
	if(document.address.street1.value == ''){
		lblError.innerHTML += "<b>&nbsp;Address is required.</b><br/><br/>";
		showError(document.address.street1);
		intPass = 0;
	}
	if(document.address.zip.value == ''){
		lblError.innerHTML += "<b>&nbsp;Zip code is required.</b><br/><br/>";
		showError(document.address.zip);
		intPass = 0;
	}
	if(document.address.country.value == ''){
		lblError.innerHTML += "<b>&nbsp;Country is required.</b><br/><br/>";
		showError(document.address.country);
		intPass = 0;
	}
	if(document.address.email.value == ''){
		lblError.innerHTML += "<b>&nbsp;Email is required.</b><br/><br/>";
		showError(document.address.email);
		intPass = 0;
	}
	if(document.address.email2 != null){
		if(document.address.email.value != document.address.email2.value){
			lblError.innerHTML += "<b>&nbsp;The two e-mail addresses you entered did not match.</b><br/><br/>";
			showError(document.address.email);
			showError(document.address.email2);
			intPass = 0;
		}
	}
	if(intPass == 0){
		lblError.style.display = '';
		location.hash = findAnchor("#error");
		return false;
	}else{
		//profile
		if(document.getElementById("profile")){
			saveaddr(sesid);
		}
		lblError.style.display = 'none';
		return true;
	}
	
}


function findAnchor(x){
	a = document.anchors
	for(i=0; i<a.length; i++)
	if(x<=a[i].name) return a[i].name
	return a[i-1].name
}

var isAvailable = false;
function checkvalue(){
	
	checkAvailable(document.register.email.value);
	
	var regEmail = new RegExp("@");
	
	var lblError = document.getElementById('lblError');
	
	lblError.style.backgroundColor = "#FFBBBB";
	lblError.style.border = "1px solid #FF0000";

	lblError.innerHTML = "<h1 style='font-size:14px'>Error :</h1>";
	var intPass = 1;
	
	if( !document.register.password.value){
		lblError.innerHTML += "<b>&nbsp;Password is Required.</b><br/><br/>";
		intPass = 0;
	}
	if( !document.register.email.value || !regEmail.test(document.register.email.value)){
		lblError.innerHTML += "<b>&nbsp;Valid Email is Required.</b><br/><br/>";
		intPass = 0;
	}
	if(document.register.password.value != document.register.cpassword.value){
		lblError.innerHTML += "<b>&nbsp;The two passwords you entered did not match.</b><br/><br/>";
		intPass = 0;
	}
	if(document.register.email.value != document.register.cemail.value){
		lblError.innerHTML += "<b>&nbsp;The two e-mail addresses you entered did not match.</b><br/><br/>";
		intPass = 0;
	}
	if(!document.register.agree.checked){
		lblError.innerHTML += "<b>&nbsp;You must agree to the Privacy Policy and Terms of Agreement of Optical4less.com.</b><br/><br/>";
		intPass = 0;
	}
	if(!isAvailable && document.register.email.value != ""){
		lblError.innerHTML += "<b>&nbsp;Your Email already exist.</b><br/><br/>";
		intPass = 0;
	}
	if(intPass == 1){
		lblError.style.display = 'none';
		return true;
	}else{
		location.hash = findAnchor("#error");
		lblError.style.display = '';
		return false;
	}
}

function agree(){
	if(document.register.agree.checked == false){
		document.register.agree.checked = true;
	}else{
		document.register.agree.checked = false;
	}
}
  		
var http_request = false;

function makeRequest(url) {

    http_request = false;

    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!http_request) {
        return false;
    }
    http_request.onreadystatechange = processRequest;
    http_request.open('GET', url, true);
    http_request.send(null);

}

function processRequest() {
	var isIE = false;
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
        	var intSelectedValue = '$intSubFilter';

			 
            if (document.implementation && document.implementation.createDocument){
				xmlDoc = http_request.responseXML;
				//In case to be the internet explorer
				isIE = false;
			} else if (window.ActiveXObject){
				var xmlString = http_request.responseText;  
				xmlDoc = new ActiveXObject("Microsoft.XMLDOM");  
				xmlDoc.async = false;  
				xmlDoc.loadXML(xmlString);  
				isIE = true;
			}
			//my code
			root_node = xmlDoc.getElementsByTagName('return');
			if(root_node.item(0).firstChild.nodeValue == "true"){
				isAvailable = true;
				document.getElementById("lblSuccess").style.display = '';
				document.getElementById("lblFail").style.display = 'none';
			}else{
				isAvailable = false;
				document.getElementById("lblSuccess").style.display = 'none';
				document.getElementById("lblFail").style.display = '';
			}
			
        } else {
			alert('err');
        }
    }
}

function checkAvailable(strID){
	if(strID != ""){
		makeRequest('Account.cgi?mode=available&id=' + strID);
	}
}
