// functions.js



function uploadimg (theform) {
	//Submit the Form
	theform.submit();
	setStatus ("Loading...", "showimg");
	}

function doneloading(theframe, theURL, theparam) {
	theloc = theURL + theparam;
	//alert("the URL " + theURL + " the parameters " + theparam);
	theframe.processajax(theloc, "showimg", "post", "");
	//theframe.document.getElementById("showimg").innerHTML = "help me";
	theframe.document.getElementById("status").style.visibility = "hidden";
	}

function completeloading(theURL)
	{
	window.location.reload();
	//processajax(theURL, "business_card", "post", "");
	


	}

	var isFade = 0;
function show_form(theform, thewidth)
	{
	var showslug = document.getElementById("mainSlug");	
	var showcopy = document.getElementById("mainCopy");
	showslug.style.display = 'block';
	showslug.style.width = thewidth + 'px';
//	if (document.all && isFade == 0)
//		{
//		isFade = 1;
//		showslug.filters.blendTrans.Apply();
//		showslug.filters.blendTrans.Play();
//		}
	showcopy.style.display = 'block';
	showcopy.style.width = thewidth;
	processajax(theform, "mainCopy", "get", "");
	}

function setStatus(theStatus, theObj)
	{
	obj = document.getElementById(theObj);
	if (obj)
		{
		obj.innerHTML = "<div id = \"status\" class = \"status\">" + theStatus + "</div>";

		}
	}

function hide_form()
	{
	var showslug = document.getElementById("mainSlug");	
	var showcopy = document.getElementById("mainCopy");
	showslug.style.display = 'none';
	showcopy.style.display = 'none';
	}
function text_counter(theDiv, theStr, theLimit)
	{
	
	var theNewCount = theLimit - theStr.length;
	document.getElementById(theDiv).innerHTML = theNewCount;
	}
function calPrice()
	{
	var showPrice = document.getElementById("billed_amount");
	var paypalPrice = document.getElementById("amount");
	var amountDue = document.getElementById("payment_due").value;
	//alert(amountDue);
	var finalAmount = Math.round((parseInt(amountDue) * 1.029 + .30)*100)/100;
	paypalPrice.value = finalAmount;
	showPrice.innerHTML = finalAmount;	

	}


////////////////////////////
// validate phone number

// 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;

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"))) {//alert(c+' '+i);
		return false;}
    }
    // All characters are numbers.
    return true;
}
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 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 checkInternationalPhone(strPhone){
//alert ('here');
var bracket=3;
strPhone=trim(strPhone);
//alert (strPhone);
if(strPhone.indexOf("+")>1) {return false; }
if(strPhone.indexOf("-")!=-1){
  bracket=bracket+1; 
  theIndex = strPhone.indexOf("(");
  //alert("bracket: "+bracket+ ' theIndex: '+theIndex);
  }
if(strPhone.indexOf("(")!=-1) //{alert ("-1");} 
if ( strPhone.indexOf("(") > bracket)
 {//alert (strPhone.indexOf("(") +" > "+ bracket);
  return false;}
var brchr=strPhone.indexOf("(");
//alert ("brchr: "+brchr);
if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+4)!=")")return false;
if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false;
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}


function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		email_label = document.getElementById('email_label');
		email = document.getElementById('email');
		if (str.indexOf(at)==-1){
		   email_label.style.color = '#ff0000';

		   alert("Invalid Email address, please enter an email in the form of yourname@domain.com.");
		   email.focus();
		   email.select();
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   email_label.style.color = '#ff0000';

		   alert("Invalid Email address, please enter an email in the form of yourname@domain.com.");
		   email.focus();
		   email.select();
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    email_label.style.color = '#ff0000';

		    alert("Invalid Email address, please enter an email in the form of yourname@domain.com.");
		   email.focus();
		   email.select();
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    document.getElementById('email_label').style.color = '#ff0000';

		    alert("Invalid Email address, please enter an email in the form of yourname@domain.com.");
		   email.focus();
		   email.select();
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    document.getElementById('email_label').style.color = '#ff0000';
		   email.focus();
		   email.select();
		    alert("Invalid Email address");
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    document.getElementById('email_label').style.color = '#ff0000';
		    alert("Invalid Email address");
		   email.focus();
		   email.select();
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		    document.getElementById('email_label').style.color = '#ff0000';
		    alert("Invalid Email address");
		   email.focus();
		   email.select();
		    return false;
		 } else {
		document.getElementById('email_label').style.color = '#000000';
 		 return true;
		}					
	}
///////////////////////////////////////

function submit_form (theForm, serverPage, theDiv) {

  	if (theForm == "register") {
	  company = document.getElementById('company');
	  first = document.getElementById('name_first');
	  first_value = document.getElementById('name_first').value;
	  first_label =  document.getElementById('first_label');
	  
 	  last = document.getElementById('name_last');
	  last_value = document.getElementById('name_last').value;
	  last_label =  document.getElementById('last_label');
	
	  phone = document.getElementById('phone');
	  phone_value = phone.value;
          phone_label = document.getElementById('phone_label');
	 
	  email = document.getElementById('email');
	  email_value =  document.getElementById('email').value;
	  email_label =  document.getElementById('email_label');

	refer = document.getElementById('refer').value;

	if (first_value == '') {
	  first_label.style.color = '#ff0000';
	  
	  alert ('Please enter your first name.');
	  first.focus();
	  return false; 
	  } else {
	    last_label.style.color = '#000000';
	      if (last_value == '') {
	        last_label.style.color = '#ff0000';
	  
	        alert ('Please enter your last name.');
	        last.focus();
	  return false; 
	  } else {
	    first_label.style.color = '#000000';
	  //  if ((isPhoneNumber (phone)) ) {
	
	if ((phone_value==null)||(phone_value=="")){
		alert("Please Enter your Phone Number (555) 555-1234");
		phone_label.style.color = '#ff0000';
	
		phone.focus();
		return false;
	}
	if (checkInternationalPhone(phone_value)==false){
		alert("Please Enter a Valid Phone Number (555) 555-1234");
		phone_label.style.color = '#ff0000';
		phone.select();
		phone.focus();
		return false;
	}
	else { phone_label.style.color = '#000000'; 
	
  	
	        if (echeck(email_value)) {
		  // a bit of clean up, I think echeck is changing the variable email from the value created earlier and the actual object reference
	       	  email = email.value;
		//	alert (email);
		  
		  str = "_submit_check_reg=1&amp;_submit_ajax=1";
		  str += "&name_first="+first_value+"&name_last="+last_value;
		  str += "&phone=";
		  str += phone.value;
		  str += "&email="+email_value+"&refer="+ refer;
	//	  window.open('https://www.paypal.com/cgi-bin/webscr');
		  processajax (serverPage, theDiv, 'post', str);
		} else { return false; }
	     }
	    //} else { return false; }
	}
	

}





     	  

	}

}
