// JavaScript Document

var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}

@else
	xmlhttp = false;
@end @*/

if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
}


visaimg = new Image(100,63)
visaimg.src = "images/cc-visa.gif"
masterimg = new Image(100,63)
masterimg.src = "images/cc-master.gif"
ameximg = new Image(100,63)
ameximg.src = "images/cc-amex.gif"
discoverimg = new Image(100,63)
discoverimg.src = "images/cc-discover.gif"
blankimg = new Image(100,63)
blankimg.src = "images/cc-blank.gif"
var montharray=new Array("January","February","March","April","May","June","July","August","September","October","November","December")

function showDate(){
    var mydate=new Date()
    var year=mydate.getYear()
    if (year < 1000) year+=1900
    var month=mydate.getMonth()
    var daym=mydate.getDate()
    if (daym<10) daym="0"+daym

	var cdate= montharray[month] + " " + daym + ", " + year
    document.getElementById("date").innerHTML = cdate
}

function deleteConfirm() {
	var agree = confirm("Are you sure you want to delete? There's no coming back...");
	if (agree)
		return true ;
	else
		return false ;
}

function isEmailValid(email) {
	var regex = /^[a-z0-9!$'*+\-_]+(\.[a-z0-9!$'*+\-_]+)*@([a-z0-9]+(-+[a-z0-9]+)*\.)+([a-z]{2}|aero|arpa|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|msp|name|net|org|pro|travel)$/;
	return regex.test(email.toLowerCase());
}

function formValidator() {
	var email = document.form.userEmail;
	var Password = document.form.userPasswd;
	var rePassword = document.form.userPasswd2;
	var name = document.form.userName;
	
	if (name.value == 0) {
		alert("Please enter your name.");
		name.focus();
		return false;
	}
	if (!isEmailValid(email.value)) {
		alert("Invalid email address. Please re-enter");
		email.focus();
		return false;
	}
	if (Password.value =="") {
		alert ("Please enter a password (min 6 characters)");
		Password.focus();
		return false;
	} else if (Password.length < 6) {
		alert ("Password must have at least 6 characters. Please Re-enter.");
		Password.focus();
		return false;
	} else if (Password.value.indexOf(" ") >= 0) {
		alert ("Space is not ALLOWED for password. Please re-enter.");
		Password.focus();
		return false;
	} else if (Password.value != rePassword.value) {
		alert ("Password do not matched. Please re-enter.");
		rePassword.focus();
		return false;
	}
}

function showPop(sel) {
	document.getElementById(sel).style.display = 'inline';
}

function closePop(sel) {
	document.getElementById(sel).style.display = 'none';
}

function addBookmark() {
	var url = "http://www.telephonenumber.com";
	var title = "TelephoneNumber.com - Place Where The Phone Numbers Live";
	if(window.sidebar){ // Firefox
          window.sidebar.addPanel(title, url,'');
    }else if(window.opera){ //Opera
          var a = document.createElement("A");
          a.rel = "sidebar";
          a.target = "_search";
          a.title = title;
          a.href = url;
          a.click();
    } else if(document.all){ //IE
          window.external.AddFavorite(url, title);
    }
}



function popDetail(sel) {
	document.getElementById('overlay2').style.display = 'inline';
	
	if (xmlhttp) {
	var url = "/srv/details.php?rand=" + sel;
	xmlhttp.open("GET", url, true);
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4) {
				var output = ''; var i; var tmp; var streets; var n;
				var vals = xmlhttp.responseText.split('||||');
				
				//var post = vals[0] + '<br />' + vals[1] + '<br />' + vals[2] + '<br />' + vals[3] + '<br />' + vals[4] + '<br />' + vals[5];
				var contact = vals[0].split('~!~');
				if(contact[0]) output = output + '<div class="rec"><label>First Name:</label><span>' + contact[0] + '</span></div>';
				if(contact[1]) output = output + '<div class="rec"><label>Last Name:</label><span>' + contact[1] + '</span></div>';
				if(contact[2]) output = output + '<div class="rec"><label>Middle Name:</label><span>' + contact[2] + '</span></div>';
				if(contact[5]) output = output + '<div class="rec"><label>Nickname:</label><span>' + contact[5] + '</span></div>';
				if(contact[8]) output = output + '<div class="rec"><label>Company:</label><span>' + contact[8] + '</span></div>';
				if(contact[9]) output = output + '<div class="rec"><label>Job Title:</label><span>' + contact[9] + '</span></div>';
				if(contact[10]) output = output + '<div class="rec"><label>Department:</label><span>' + contact[10] + '</span></div>';
				if(contact[12]) output = output + '<div class="rec"><label>Birthday:</label><span>' + contact[12] + '</span></div>';
				if(contact[13]) output = output + '<div class="rec"><label>Note:</label><span>' + contact[13] + '</span></div>';
				
				var phones = vals[1].split('^!^');
				i = 0;
				while(phones[i]) {
					tmp = phones[i].split('~!~');
					output = output + '<div class="rec"><label>Phone:</label><span>' + tmp[1] + '</span></div>';
					i++;
				}
				
				var emails = vals[2].split('^!^');
				i = 0;
				while(emails[i]) {
					tmp = emails[i].split('~!~');
					output = output + '<div class="rec"><label>Email:</label><span>' + tmp[1] + '</span></div>';
					i++;
				}
				
				var instant = vals[3].split('^!^');
				i = 0;
				while(instant[i]) {
					tmp = instant[i].split('~!~');
					output = output + '<div class="rec"><label>' + tmp[0] + '</label><span>' + tmp[1] + '</span></div>';
					i++;
				}
				
				var url = vals[4].split('^!^');
				i = 0;
				while(url[i]) {
					tmp = url[i].split('~!~');
					output = output + '<div class="rec"><label>Website:</label><span>' + tmp[1] + '</span></div>';
					i++;
				}
				
				var addr = vals[5].split('^!^');
				i = 0;
				while(addr[i]) {
					tmp = addr[i].split('~!~');
					
					streets = tmp[1].split('***');
					var n = 0;
					while(streets[n]) {
						if(n == 0) {
							output = output + '<div class="rec"><label>Address:</label><span>' + streets[n] + '</span></div>';
						} else {
							output = output + '<div class="rec"><label>&nbsp;</label><span>' + streets[n] + '</span></div>';
						}
						n++;
					}
					output = output + '<div class="rec"><label>&nbsp;</label><span>' + tmp[3] + ' ' + tmp[4] + ' ' + tmp[5] + '</span></div>';
					output = output + '<div class="rec"><label>&nbsp;</label><span>' + tmp[0] + '</span></div>';
					i++;
				}
			}
			document.getElementById('contact').innerHTML = output;
		}
	}
	xmlhttp.send(null)
}

function setImg(val) {
	if(val.selectedIndex == 1) {
		document.getElementById('ccimg').src = visaimg.src;
	} else if(val.selectedIndex == 2) {
		document.getElementById('ccimg').src = masterimg.src;
	} else if(val.selectedIndex == 3) {
		document.getElementById('ccimg').src = discoverimg.src;
	} else if(val.selectedIndex == 4) {
		document.getElementById('ccimg').src = ameximg.src;
	} else {
		document.getElementById('ccimg').src = blankimg.src;
	} 
}

function clearF(val) {
	document.getElementById(val).value = '';
	document.getElementById(val + 'T').style.display = 'inline';
}

function clearQ(val) {
	document.getElementById(val).style.display = 'none';
}

function isEmailValid(email) {
	var regex = /^[a-z0-9!$'*+\-_]+(\.[a-z0-9!$'*+\-_]+)*@([a-z0-9]+(-+[a-z0-9]+)*\.)+([a-z]{2}|aero|arpa|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|msp|name|net|org|pro|travel)$/;
	return regex.test(email.toLowerCase());
}

function formValidator() {
	
	if(document.getElementById('ccType').value.length < 1) {
		alert("Please select Card Type");
		document.getElementById('ccType').focus();
		return false;
	}
	
	var ccnum =  document.getElementById('ccNo').value.replace(/[\\ A-Za-z!"£$%^&*+_={}()-.;:'@#~,¦\/<>?|`¬\]\[]/g,'');
	if(!(Mod10(ccnum))) {
		alert("This doesn't look like a valid Creditcard Number");
		document.getElementById('ccNo').focus();
		return false;
	}
	
	if(document.getElementById('ccExpY').value.length < 1) {
		alert("Please select Creditcard Expiration Year");
		document.getElementById('ccExpY').focus();
		return false;
	}
	
	if(document.getElementById('ccExpM').value.length < 1) {
		alert("Please select Creditcard Expiration Month");
		document.getElementById('ccExpM').focus();
		return false;
	}
	
	if((document.getElementById('ccName').value.length < 3) || (document.getElementById('ccName').value == 'Cardholder Name')) {
		alert("Please enter Cardholder Name");
		document.getElementById('ccName').focus();
		return false;
	}
	
	var email = document.getElementById('ccEmail');
	if (!isEmailValid(email.value)) {
		alert("Invalid email address. Please re-enter");
		email.focus();
		return false;
	}
	
	if((document.getElementById('ccAddr').value.length < 3) || (document.getElementById('ccAddr').value == 'Billing Address')) {
		alert("Please enter Billing Address");
		document.getElementById('ccAddr').focus();
		return false;
	}
	
	if((document.getElementById('ccCity').value.length < 2) || (document.getElementById('ccCity').value == 'City')) {
		alert("Please enter Billing City");
		document.getElementById('ccCity').focus();
		return false;
	}
	
	if((document.getElementById('ccState').value.length < 2) || (document.getElementById('ccState').value == 'State')) {
		alert("Please enter Billing State");
		document.getElementById('ccState').focus();
		return false;
	}
	
	if((document.getElementById('ccZip').value.length < 5) || (document.getElementById('ccZip').value == 'ZIP')) {
		alert("Please enter Billing Zipcode");
		document.getElementById('ccZip').focus();
		return false;
	}
	
	document.getElementById('sub-btn').style.display = 'none';
	document.getElementById('sub-wait').style.display = 'inline';
	
	return true;
}



function Mod10(ccNumb) {  // v2.0
	var valid = "0123456789"  // Valid digits in a credit card number
	var len = ccNumb.length;  // The length of the submitted cc number
	var iCCN = parseInt(ccNumb);  // integer of ccNumb
	var sCCN = ccNumb.toString();  // string of ccNumb
	sCCN = sCCN.replace (/^\s+|\s+$/g,'');  // strip spaces
	var iTotal = 0;  // integer total set at zero
	var bNum = true;  // by default assume it is a number
	var bResult = false;  // by default assume it is NOT a valid cc
	var temp;  // temp variable for parsing string
	var calc;  // used for calculation of each digit

	// Determine if the ccNumb is in fact all numbers
	for (var j=0; j<len; j++) {
  		temp = "" + sCCN.substring(j, j+1);
  		if (valid.indexOf(temp) == "-1"){
			bNum = false;
		}
	}

	// if it is NOT a number, you can either alert to the fact, or just pass a failure
	if(!bNum){
  	/*alert("Not a Number");*/
	bResult = false;
	}

	// Determine if it is the proper length 
	if((len == 0)&&(bResult)){  // nothing, field is blank AND passed above # check
  		bResult = false;
	} else{  // ccNumb is a number and the proper length - let's see if it is a valid card number
  		if(len >= 15){  // 15 or 16 for Amex or V/MC
    		for(var i=len;i>0;i--){  // LOOP throught the digits of the card
      			calc = parseInt(iCCN) % 10;  // right most digit
      			calc = parseInt(calc);  // assure it is an integer
     			iTotal += calc;  // running total of the card number as we loop - Do Nothing to first digit
      			i--;  // decrement the count - move to the next digit in the card
      			iCCN = iCCN / 10;                               // subtracts right most digit from ccNumb
      			calc = parseInt(iCCN) % 10 ;    // NEXT right most digit
      			calc = calc *2;                                 // multiply the digit by two
      			// Instead of some screwy method of converting 16 to a string and then parsing 1 and 6 and then adding them to make 7,
      			// I use a simple switch statement to change the value of calc2 to 7 if 16 is the multiple.
      			switch(calc){
        			case 10: calc = 1; break;       //5*2=10 & 1+0 = 1
        			case 12: calc = 3; break;       //6*2=12 & 1+2 = 3
        			case 14: calc = 5; break;       //7*2=14 & 1+4 = 5
        			case 16: calc = 7; break;       //8*2=16 & 1+6 = 7
        			case 18: calc = 9; break;       //9*2=18 & 1+8 = 9
        			default: calc = calc;           //4*2= 8 &   8 = 8  -same for all lower numbers
      			}                                               
    			iCCN = iCCN / 10;  // subtracts right most digit from ccNum
    			iTotal += calc;  // running total of the card number as we loop
  			}  // END OF LOOP
  			if ((iTotal%10)==0){  // check to see if the sum Mod 10 is zero
    			bResult = true;  // This IS (or could be) a valid credit card number.
  			} else {
    			bResult = false;  // This could NOT be a valid credit card number
    		}
  		}
	}
  	return bResult; // Return the results
}

function loginError() {
	alert('Sorry, login failed...\nPlease try again.');
}
