// JavaScript Document

//--------------------
//	Ajax Handles
//--------------------
function GetXmlHttpObject() // makes the object
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 // Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}



function swappic(replaceid, newpic) {
	if (newpic != $('cur_pic').innerHTML) {
		$(replaceid).src = newpic;
		$('cur_pic').innerHTML = newpic;
	/*
	new Effect.Opacity(replaceid, {duration:0, from:1, to:0,
					   afterFinish: function () {
							$(replaceid).src = 'includes/pics/'+newpic+'.jpg';;
							$('cur_pic').innerHTML = newpic;
							new Effect.Opacity(replaceid, {duration:1, from:0, to:1});
					   }
	})*/
	}
	
}


function fullview() {
	$('footer').style.visibility = 'hidden';
	$('special').style.visibility = 'hidden';
	var pic = $('cur_pic').innerHTML;
	
	$('fullview_container').innerHTML = "<a href='javascript:closeform()' class='close'>click here to zoom out</a><div id='fullview_pic' align='center'><img src='"+pic+"' /></div>";
	$('fullview_container').style.display = 'block';
	new Effect.Opacity('fullview_container', {duration:.5, from:0, to:1});
}

function closeform() {
	$('special').style.visibility = 'visible';
	$('footer').style.visibility = 'visible';

	new Effect.Opacity('fullview_container', {duration:.5, from:1, to:0,
					   afterFinish: function () {
							$('fullview_container').innerHTML = '';
							$('fullview_container').style.display = 'none';
					   }
   });
	
}



function form(formname, id) {
	$('footer').style.visibility = 'hidden';
	$('special').style.visibility = 'hidden';

	$('fullview_container').innerHTML = "<a href='javascript:closeform()' class='close'>click here to close form</a><div id='insideform' align='center'><div id='loader'><img src='includes/images/loading.gif' alt='loading' /></div><div id='blackform'></div>";
	$('fullview_container').style.display = 'block';
	if ($('fullview_container').style.opacity == 0) {
	new Effect.Opacity('fullview_container', {duration:.5, from:0, to:1,
						afterFinish: function() {
							new Ajax.Request('includes/scripts/forms.php?form='+formname+'&id='+id, {
											 onComplete: function(transport) {
												$('loader').innerHTML = "";;
												new Effect.Opacity('blackform', {duration:0, from:1, to:0});
												$('blackform').innerHTML = transport.responseText;
												new Effect.Opacity('blackform', {duration:.3, from:0, to:1});
											 }
							});
							
						}
					   
					   
	});
	} else {
		new Ajax.Request('includes/scripts/forms.php?form='+formname+'&id='+id, {
						 onComplete: function(transport) {
							$('loader').innerHTML = "";;
							new Effect.Opacity('blackform', {duration:0, from:1, to:0});
							$('blackform').innerHTML = transport.responseText;
							new Effect.Opacity('blackform', {duration:.3, from:0, to:1});
						 }
		});
	}



}


function handleform(url, names, id, radio, check) {
	window.scroll(0,0);

	new Effect.Opacity('blackform', {duration:0, from:1, to:0});
	$('loader').innerHTML = "<img src='includes/images/loading.gif' alt='loading' />";
	xmlHttp=GetXmlHttpObject()
	
	if (xmlHttp==null)
	  {
	  alert ("Browser does not support HTTP Request")
	  return
	  } 
	var url="includes/scripts/forms.php?form="+url;
	if (id) {
		url += "&id="+id;	
	}
	
	xmlHttp.onreadystatechange=stateChanged_form;

	xmlHttp.open("POST",url,true);
	var poststr;
	var newnames = names.split('\,');
		thetext = '“Don’t you know what It’s like to be afraid?”';
	var theval;
		
	for (i=0;i<newnames.length;i++) {
		var thevalue = document.getElementById(newnames[i]).value;
		if (i ==0) {
			theval = newnames[i]+"="+encodeURI(thevalue);
			theval = theval.replace(/&/, 'amp;');
			poststr = theval;
		} else {
			theval = newnames[i]+"="+encodeURI(thevalue);
			theval = theval.replace(/&/, 'amp;');
			poststr	+= "&"+theval;
		}
	}
	if (radio) { // radioboxes
		newradio = radio.split('\,');
		for (i=0;i<newradio.length;i++) {
			checkvalue = document.getElementsByName(newradio[i]);
			for(a=0;a<checkvalue.length;a++) {
				if (checkvalue[a].checked == true) {
					poststr += "&"+newradio[i]+"="+encodeURI(checkvalue[a].value);	
				}
			}
		}
	}
	
	
	
	if (check) { // build the checkbox
		checkbox = document.getElementsByName(check);
		checkval = '';
		a = 0;
		for(i=0;i<checkbox.length;i++) {
			if (checkbox[i].checked) {
				if (a > 0) {
					checkval += ','+checkbox[i].value;
				} else {
					checkval += checkbox[i].value;	
					a++;
				}
			}
		}
		poststr +="&"+check+"="+encodeURI(checkval);
	}
	
	
	//document.getElementById('blackdrop').innerHTML = "<br /><br /><br /><div align='center'><img src='images/ajax-loader.gif' /><br /><br />Processing Form</div>";
      xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xmlHttp.setRequestHeader("Content-length", poststr.length);
      xmlHttp.setRequestHeader("Connection", "close");
      xmlHttp.send(poststr);
	xmlHttp.send(poststr);

}

function stateChanged_form() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		// is this a message style
		var thestring = xmlHttp.responseText;
	if (thestring.search(/(close){1}\/([A-Za-z ])*(\/)*([A-Za-z ])*/) == -1) {
			$('blackform').innerHTML=xmlHttp.responseText;
		} else {
			thestring = thestring.split('/');

			if (thestring.length == 3) {
				//closeform(2, thestring[1], thestring[2]);
				window.location = thestring[2];
			} else {
				//closeform(2, thestring[1]);
				window.location = window.location;
			}
		}
		$('loader').innerHTML = '';
		new Effect.Opacity('blackform', {duration:.2, from:0, to:1});
 	}
}

function otherbox(which,check) {
	if (which.value == 'other') {
		$(check).checked=true;
		which.style.color = '#000';
		which.value = '';
	}
}

function otherboxout(which,check) {
	if (which.value == '' || which.value == 'other') {
		$(check).checked=false;
		which.style.color = '#999';
		which.value = 'other';
	}
}

function openwindow(url) {
	window.open(url, null, "height=650, width=600, left=20px, top=20px,scrollbars=yes,resizable=yes,titlebar=no");
	//window.open(url, "Design Process", "height=650, width=600, left=20px, top=20px,scrollbars=yes,resizable=yes");	
}

function sendorder() {
	$('paypal').innerHTML = '';
	$('form').innerHTML = "<h3>PROCESSING ORDER...</h3>";	
}

function defaultcolor(id,color) {
	$(id).value = color;	
}

function emailValidator(email){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(email.match(emailExp)){
		return true;
	}else{
		return false;
	}
}

function realzip(zip) {
	var zipExp = /^[0-9]{5}$/
	if (zip.match(zipExp)) {
		return true;
	} else {
		return false;	
	}
}

function realphone(phone) {
	var checkphone = phone;
	checkphone = checkphone.replace(/\-/g,'');
	checkphone = checkphone.replace(/\)/g,'');
	checkphone = checkphone.replace(/\(/g,'');
	checkphone = checkphone.replace(/ /g,'');

	var phoneExp = /^[0-9]{10}$/;
	if (checkphone.match(phoneExp)) {
		return true;	
	} else {
		return false;
	}
}

function badcolors(which) {
	var red = '#e47d78';
	$(which).style.background = red;
	$(which).style.color = '#fff';
	$(which).blur();
}

function validateform() {
	var Requireds = new Array();
	Requireds[0] = $('firstName');
	Requireds[1] = $('lastName');
	Requireds[2] = $('email');
	Requireds[3] = $('phone');
	Requireds[4] = $('address1');
	Requireds[5] = $('city');
	Requireds[6] = $('state');
	Requireds[7] = $('zip');
	Requireds[8] = $('creditCardNumber');
	Requireds[9] = $('cvv2Number');
	
	var First = $('firstName').value;
	var Last = $('lastName').value;
	var Email = $('email').value;
	var realEmail = emailValidator(Email);
	var Phone = $('phone').value;
	var realPhone = realphone(Phone);
	var Address1 = $('address1').value;
	var Address2 = $('address2').value;
	var City = $('city').value;
	var State = $('state').value;
	var Zip = $('zip').value;
	var realZip = realzip(Zip);
	
	var creditCardNumber = $('creditCardNumber').value;
	var cvv2Number = $('cvv2Number').value;
	
	var theerror = false;
	var sendform = false;
	
	
	if (First && Last && Email && realEmail && Phone && realPhone && Address1 && City && State && Zip && realZip && creditCardNumber && cvv2Number) {
		return true;
	} else {
		$('error').innerHTML = "The red fields are either missing or invalid";
		for(i=0;i<Requireds.length;i++) {
			if (!Requireds[i].value) {
				badcolors(Requireds[i]);
			}
		}
		
		if (!realEmail) {
			badcolors($('email'));
		}
		if (!realPhone) {
			badcolors($('phone'));
		}
		if (!realZip) {
			badcolors($('zip'));
		}

	}

	
	return false;
}

function fixfield(which) {
	which.style.background = '#fff';
	which.style.color = '#000';
}
