function parameters(){
var l = document.location.href;
//l="test?order=123&test=9876&qaz=dedee";
var m=l.split("?");
a=new Array();
if (m.length==2){
var str=m[1];
x=str.split("&");
for(i=0;i<x.length;i++){
y=x[i].split("=");
a[y[0]]=y[1]; //key version
a[i]=new Array(y[0],y[1]); //num version
}
}
return a;
}



function parameters_to_string(input){
x="";
for(i=0;i<input.length;i++){
x+=input[i][0]+"="+input[i][1]+"&";
}
x=x.substr(0,x.length-1);
return x;
}


function parameters_replace(key,newvalue){
var p=parameters();
var e=0;
for(i=0;i<p.length;i++){
	if (p[i][0]==key){
	p[i][1]=newvalue;	
	e=1;
	}
}
if (e==0){
p[p.length]=new Array(key,newvalue);	
}
p[key]=newvalue;

return p;	
}


function order(obj,key,newvalue){
obj.value;
if (key!=null){
x=parameters_to_string(parameters_replace(key,obj.value));
var l = document.location.href;
var m=l.split("?");
if (m.length<=2){
document.location.href=(m[0]+"?"+x);
}
}	
}

function displayimage(obj,link){
f=$(obj).src=link;
return false;	
}

function jump(to){
window.location.hash=to;
return false;
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function numbersonly(myfield, e, dec){var key;var keychar;if (window.event)   key = window.event.keyCode;else if (e)   key = e.which;else   return true;keychar = String.fromCharCode(key);// control keysif ((key==null) || (key==0) || (key==8) ||    (key==9) || (key==13) || (key==27) )   return true;// numberselse if ((("0123456789").indexOf(keychar) > -1))   return true;// decimal point jumpelse if (dec && (keychar == "."))   {   myfield.form.elements[dec].focus();   return false;   }else   return false;}



 
function check_numbers(theForm) {
var output = "";
var allowed= new Array("1","2","3","4","5","6","7","8","9","0");
var keys = theForm.value;
var keylength = keys.length;
for (i=0; i<=keylength-1; i++){
var thekey = keys.charAt(i);

var exists = 0;
for (x in allowed){
var keyok = allowed[x];
if (thekey==keyok){
var exists = 1;
}
}

if (exists != 0){
var output = output + thekey;
}
}
theForm.value=output;
}

var Base64 = {

   // private property
   _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

   // public method for encoding
   encode : function (input) {
       var output = "";
       var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
       var i = 0;

       input = Base64._utf8_encode(input);

       while (i < input.length) {

           chr1 = input.charCodeAt(i++);
           chr2 = input.charCodeAt(i++);
           chr3 = input.charCodeAt(i++);

           enc1 = chr1 >> 2;
           enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
           enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
           enc4 = chr3 & 63;

           if (isNaN(chr2)) {
               enc3 = enc4 = 64;
           } else if (isNaN(chr3)) {
               enc4 = 64;
           }

           output = output +
           this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
           this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

       }

       return output;
   },

   // public method for decoding
   decode : function (input) {
       var output = "";
       var chr1, chr2, chr3;
       var enc1, enc2, enc3, enc4;
       var i = 0;

       input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

       while (i < input.length) {

           enc1 = this._keyStr.indexOf(input.charAt(i++));
           enc2 = this._keyStr.indexOf(input.charAt(i++));
           enc3 = this._keyStr.indexOf(input.charAt(i++));
           enc4 = this._keyStr.indexOf(input.charAt(i++));

           chr1 = (enc1 << 2) | (enc2 >> 4);
           chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
           chr3 = ((enc3 & 3) << 6) | enc4;

           output = output + String.fromCharCode(chr1);

           if (enc3 != 64) {
               output = output + String.fromCharCode(chr2);
           }
           if (enc4 != 64) {
               output = output + String.fromCharCode(chr3);
           }

       }

       output = Base64._utf8_decode(output);

       return output;

   },

   // private method for UTF-8 encoding
   _utf8_encode : function (string) {
       string = string.replace(/\r\n/g,"\n");
       var utftext = "";

       for (var n = 0; n < string.length; n++) {

           var c = string.charCodeAt(n);

           if (c < 128) {
               utftext += String.fromCharCode(c);
           }
           else if((c > 127) && (c < 2048)) {
               utftext += String.fromCharCode((c >> 6) | 192);
               utftext += String.fromCharCode((c & 63) | 128);
           }
           else {
               utftext += String.fromCharCode((c >> 12) | 224);
               utftext += String.fromCharCode(((c >> 6) & 63) | 128);
               utftext += String.fromCharCode((c & 63) | 128);
           }

       }

       return utftext;
   },

   // private method for UTF-8 decoding
   _utf8_decode : function (utftext) {
       var string = "";
       var i = 0;
       var c = c1 = c2 = 0;

       while ( i < utftext.length ) {

           c = utftext.charCodeAt(i);

           if (c < 128) {
               string += String.fromCharCode(c);
               i++;
           }
           else if((c > 191) && (c < 224)) {
               c2 = utftext.charCodeAt(i+1);
               string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
               i += 2;
           }
           else {
               c2 = utftext.charCodeAt(i+1);
               c3 = utftext.charCodeAt(i+2);
               string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
               i += 3;
           }

       }

       return string;
   }

} 


function check_order_form(theForm) {
	
	var checked = false, e, i = 0;
	
	while (e = theForm.elements[i++]) {
		if (e.type == 'checkbox' && e.checked) checked = true;
	}
	if (!checked) {alert ("Please choose at least one service.");
		return false;
	}

	
	if (theForm.forename.value == '') {
			alert("Please enter your first name.");
			theForm.forename.focus();
			return false;
	}
	
	if (theForm.surname.value == '') {
			alert("Please enter your last name.");
			theForm.surname.focus();
			return false;
	}
	
	if (theForm.telephone.value == '') {
			alert("Please enter your telephone number.");
			theForm.telephone.focus();
			return false;
	}
	
	
	if (theForm.email.value == '') {
			alert("Please enter your e-mail address.");
			theForm.email.focus();
			return false;
	}
			var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		
	if (filter.test(theForm.email.value) != true){
			theForm.email.focus();
			alert("Your e-mail address is not valid! Please try again.");
			return false;	
	}
	
	if (theForm.streetaddress.value == '') {
			alert("Please enter a street address.");
			theForm.streetaddress.focus();
			return false;
	}
	
	if (theForm.town.value == '') {
			alert("Please enter your town/city.");
			theForm.town.focus();
			return false;
	}
	
	if (theForm.postcode.value == '') {
			alert("Please enter your post/zip code.");
			theForm.postcode.focus();
			return false;
	}
	
	if (theForm.cardnumber.value == '') {
			alert("Please enter your credit card number.");
			theForm.cardnumber.focus();
			return false;
	}
	if (theForm.cardtype.value == "AMEX" && theForm.cardnumber.value.length!=15) {
			alert("Card number is invalid! Check digits again.");
			theForm.cardtype.focus();
			return false;
	}
	
	if (theForm.cardtype.value == "VISA" && theForm.cardnumber.value.length!=16) {
			alert("Card number is invalid! Check digits again.");
			theForm.cardnumber.focus();
			return false;
	}
	
	if(theForm.cardtype.value == "Mastercard" && theForm.cardnumber.value.length!=16){
			alert("Card number is invalid! Check digits again.");
			theForm.cardnumber.focus();
			return false;
	}
	if(theForm.cardtype.value == "Maestro" && theForm.cardnumber.value.length!=18){
			alert("Card number is invalid! Check digits again.");
			theForm.cardnumber.focus();
			return false;
	}
			
	if (theForm.cvv.value.length<3) {
			alert("CVV number is too short!\nMust be at least 3 digits.");
			theForm.cvv.focus();
			return false;
	}
	
	if (theForm.cardtype.value == "AMEX" && theForm.cvv.value.length<4 ) {
			alert("AMEX CVV needs to be 4 digits!");
			theForm.cvv.focus();
			return false;
	}
		
	var message="<b>Personal Details</b>\n";
	var fullname = theForm.forename.value+" "+theForm.surname.value;
	var email = theForm.email.value;
	var company = theForm.leadgenerator.value;
	var agentservice = "";

	
	for (i = 0; i < theForm.options.length; i++){
      if (theForm.options[i].checked){
         agentservice = agentservice + theForm.options[i].value;
         
         // alert(theForm.options.length); // always 2
              
              if(i<theForm.options.length-1) {
              	agentservice = agentservice+"|";
              
              }
      } // end first if
	}
	
	if(agentservice.substr(agentservice.length-1,agentservice.length)=="|"){
		agentservice=agentservice.substr(0,agentservice.length-1);
	
	}
	
	//message+="Lead generated: "+theForm.leadgenerator.value+"\n\n";
	message+="Name: "+theForm.forename.value+" "+theForm.surname.value+"\n\n";
	message+="Email: "+theForm.email.value+"\n";
	message+="Telephone: "+theForm.telephone.value+"\n";
	
		if(theForm.companyname.value!=""){
			message+="Company Name: "+theForm.companyname.value+"\n";
		}
	
	message+="Address: "+theForm.streetaddress.value+"\n";
	message+="Town: "+theForm.town.value+"\n";
	message+="Postal/Zip code: "+theForm.postcode.value+"\n";
		if(theForm.county.value!=""){
			message+="County: "+theForm.county.value+"\n";
		}
	message+="Country: "+theForm.country.value+"\n\n\n";
	message+="<b>Credit card information</b>\n\n";
	message+="Card type: "+theForm.cardtype.value+"\n";
	message+="Card number: "+theForm.cardnumber.value+"\n";
	message+="Expiry: "+theForm.expmonth.value+"/"+theForm.expyear.value+"\n";
	message+="CVV Code: "+theForm.cvv.value+"\n\n";
	message+="Comments: "+theForm.comments.value+"\n\n";
	
document.getElementById('moreinfocontainer').style.display = "none";
document.getElementById('sendingdetails').style.display = "inline";
	
//message+="Further info:\n\n "+theForm.furtherinfo.value+"\n\n\n";

//var refer=theForm.referer.value;

$.ajax({
  type: "POST",
  url: "https://domaingod.com/seriousgroup/ishopstudio.com/factory/ajax.php",
  data:  { trigger: "submitorder", message: Base64.encode(message),name: fullname,email: email, company: company,options:agentservice },
  cache: false,
  success: function(html){
   $("gcontent").html(html);
    }

 });
		theForm.forename.value="";
		theForm.surname.value="";
		theForm.telephone.value="";
		theForm.email.value="";
		theForm.companyname.value="";
		theForm.streetaddress.value="";
		theForm.town.value="";
		theForm.county.value="";
		theForm.postcode.value="";
		theForm.cardnumber.value="";
		theForm.cvv.value="";
		theForm.comments.value="";
		
document.getElementById('sendingdetails').style.display = "none";
document.getElementById('thankyou').style.display = "inline";
		
	return false;

}