// JavaScript Document
function checkForm(){
	var isName = $('#txtName').val();
	var isService = $('#cmbService').val();
	var isPhone = $('#txtPhone').val();
	var isEmail= $('#txtEmail').val();
	var isMessage= $('#txtComments').val();
	var phoneGo=false;
	var emailGo=false;
	
	if (isName==""){
		alert('You did not provide a valid name');
		return false;};
	if (isPhone	!=""){
		minus = isPhone.charAt(3);
		if(minus=="-"){
			minus=isPhone.charAt(7);
			if(minus=="-" && isPhone.length==12){
				phoneGo=true;
			};
		}else{
			
		}	
			if(phoneGo==false){
			alert('You did not supply a valid Phone number \nUse the Format\n xxx-xxx-xxxx');
			return false
			}
	}else{
		
		alert('You did not supply a valid Phone number \nUse the Format\n xxx-xxx-xxxx');
			return false
		
		
	};
	
	
	if (isEmail==""){
		alert('You did not supply a valid email adress');
		return false
		
		}else{
			var a = isEmail;
			var filter =/^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
		//if it's valid email
		if(filter.test(a)){
			
			goEmail=true;
		}
		//if it's NOT valid
		else{
			alert('You will need to enter in a valid email.');
			return false;
		}

			
		};
	
	if( $('#noSpam:checked').val() !=undefined){
		$(".HiddenPanel").toggle("fast");
		return true;
	}else{
		alert('You need to accept our legal terms in order to send this email');
		return false;
	}
}


var webAddress;
var cookieName="acs4uClient";
function getURLNow(){
	
	webAddress = (document.location.href);
	if((webAddress.indexOf( "/clientportalddd/" ) > -1)&&(webAddress.indexOf( "/clientportal/index.php" ) == -1)){
		
		
		var isCookie = getCookie(cookieName);
		var toURL = isCookie.split('++-++');
		if(isCookie.indexOf( "hasVerified" ) > -1){
			delCookie(cookieName);
			setCookie(cookieName,"hasVerfied","/",3);
			if(webAddress.indexOf(toURL) !=-1){
				
			}else{
				//redirect
				if(webAddress.indexOf('clientportal/all')!=-1){
					
				}else{
					window.location="http://acs4u.com/clientportal/acs4u/clientportal/index.php";	
				}
			}
		
		}else{
			//redirect
			window.location="http://acs4u.com/clientportal/acs4u/clientportal/index.php";	
		}
		

		 	
	
	}
}




function sendCookie(forwardURL){
	setCookie(cookieName,"hasVerfied++-++"+forwardURL,"/",3);
	window.location=forwardURL;
};



function getCookie(c_name)
{
	
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}



//function getCookie(cookieName){
//alert('getting cookie');
//	// First we check to see if there is a cookie stored.
//	// Otherwise the length of document.cookie would be zero.
//
//	if (document.cookie.length > 0){
//
//		// Second we check to see if the cookie's name is stored in the
//		// "document.cookie" object for the page.
//
//		// Since more than one cookie can be set on a
//		// single page it is possible that our cookie
//		// is not present, even though the "document.cookie" object
//		// is not just an empty text.
//		// If our cookie name is not present the value -1 is stored
//		// in the variable called "begin".
//
//		begin = document.cookie.indexOf(cookieName+"=");
//		if (begin != -1){ // Note: != means "is not equal to"
//
//			// Our cookie was set.
//			// The value stored in the cookie is returned from the function.
//
//			begin += cookieName.length+1;
//			end = document.cookie.indexOf(";", begin);
//			if (end == -1) end = document.cookie.length;
//				return unescape(document.cookie.substring(begin, end)); 
//			}
//		}
//
//		return "null";
//
//		// Our cookie was not set.
//		// The value "null" is returned from the function.
//
//};



function isPass(toCheck){
	userArray=toCheck.split("\n");
	
};

function setCookie(NameOfCookie, value, expirehours){
	alert('setting cookie');
	// Three variables are used to set the new cookie.
	// The name of the cookie, the value to be stored,
	// and finally the number of days until the cookie expires.
	// The first lines in the function convert
	// the number of days to a valid date.

	var ExpireDate = new Date ();
	ExpireDate.setTime(ExpireDate.getTime() + (expirehours  * 3600 * 1000));
	alert('time set');
	// The next line stores the cookie, simply by assigning
	// the values to the "document.cookie" object.
	// Note the date is converted to Greenwich Mean time using
	// the "toGMTstring()" function.

	document.cookie = NameOfCookie + "=" + escape(value) + ((expirehours == null) ? "" : "; expires=" + ExpireDate.toGMTString());
	alert('cookie set');
};

function delCookie(NameOfCookie){

	// The function simply checks to see if the cookie is set.
	// If so, the expiration date is set to Jan. 1st 1970.

	if (getCookie(NameOfCookie)!="null") {
		document.cookie = NameOfCookie + "=" +"; expires=Thu, 01-Jan-70 00:00:01 GMT";
		alert('deleted cookie');
	}
};


function decrypted(str){
       var decrypted_str = "";
    
       for(c = 0; c < str.length; c ++){
          if(str.charCodeAt(c) == 32){
             decrypted_str += " ";
             continue;
          }
    
          decrypted_str += String.fromCharCode(c ^ str.charCodeAt(c));
       }
       return decrypted_str;
    };
	
	function buildPicture(inPut){
		outPut = decrypt(inPut,'stayclassy');
		return outPut;	
		
	}
	
	function buildPicture2(inPut){
		outPut = encrypt(inPut,'stayclassy');
		return outPut;	
		
	}
	
	function buildPicture3(inPut,otherLine){
		outPut = encrypt(inPut,otherLine);
		return outPut;	
		
	}
	function encrypted(str){
		
       var encrypted_str = "";
      
       for(c = 0; c < str.length; c ++){
          if(str.charCodeAt(c) == 32){
             encrypted_str += " ";
             continue;
          }
    
            encrypted_str += String.fromCharCode((str.charCodeAt(c) ^ c));
       }
       return encrypted_str;
    };
    
	
function encrypt(str, pwd) {
  if(pwd == null || pwd.length <= 0) {
    alert("Please enter a password with which to encrypt the message.");
    return null;
  }
  var prand = "";
  for(var i=0; i<pwd.length; i++) {
    prand += pwd.charCodeAt(i).toString();
  }
  var sPos = Math.floor(prand.length / 5);
  var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos*2) + prand.charAt(sPos*3) + prand.charAt(sPos*4) + prand.charAt(sPos*5));
  var incr = Math.ceil(pwd.length / 2);
  var modu = Math.pow(2, 31) - 1;
  if(mult < 2) {
    alert("Algorithm cannot find a suitable hash. Please choose a different password. \nPossible considerations are to choose a more complex or longer password.");
    return null;
  }
  var salt = Math.round(Math.random() * 1000000000) % 100000000;
  prand += salt;
  while(prand.length > 10) {
    prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();
  }
  prand = (mult * prand + incr) % modu;
  var enc_chr = "";
  var enc_str = "";
  for(var i=0; i<str.length; i++) {
    enc_chr = parseInt(str.charCodeAt(i) ^ Math.floor((prand / modu) * 255));
    if(enc_chr < 16) {
      enc_str += "0" + enc_chr.toString(16);
    } else enc_str += enc_chr.toString(16);
    prand = (mult * prand + incr) % modu;
  }
  salt = salt.toString(16);
  while(salt.length < 8)salt = "0" + salt;
  enc_str += salt;
  return enc_str;
}

function decrypt(str, pwd) {
  if(str == null || str.length < 8) {
    alert("A salt value could not be extracted from the encrypted message because it's length is too short. The message cannot be decrypted.");
    return;
  }
  if(pwd == null || pwd.length <= 0) {
    alert("Please enter a password with which to decrypt the message.");
    return;
  }
  var prand = "";
  for(var i=0; i<pwd.length; i++) {
    prand += pwd.charCodeAt(i).toString();
  }
  var sPos = Math.floor(prand.length / 5);
  var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos*2) + prand.charAt(sPos*3) + prand.charAt(sPos*4) + prand.charAt(sPos*5));
  var incr = Math.round(pwd.length / 2);
  var modu = Math.pow(2, 31) - 1;
  var salt = parseInt(str.substring(str.length - 8, str.length), 16);
  str = str.substring(0, str.length - 8);
  prand += salt;
  while(prand.length > 10) {
    prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();
  }
  prand = (mult * prand + incr) % modu;
  var enc_chr = "";
  var enc_str = "";
  for(var i=0; i<str.length; i+=2) {
    enc_chr = parseInt(parseInt(str.substring(i, i+2), 16) ^ Math.floor((prand / modu) * 255));
    enc_str += String.fromCharCode(enc_chr);
    prand = (mult * prand + incr) % modu;
  }
  return enc_str;
}
    
