function createRequestObject(){
	var http;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
		http = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		http = new XMLHttpRequest();
	}
	return http;
}

var http = createRequestObject();

function emailFriend(couponid){
	var id = "mail" + couponid;
	//alert("ID: " + id);
	var obj = document.getElementById(id);
	var posx = findPosX(obj);
	var posy = findPosY(obj);
	//alert("pos x " + posx + " - pos y " + posy);
	var height = getPageHeight();
	//alert("height " + height + " - posy: " + posy);
	var left = posx - 420;
	if(posy + 200 > height)
		var top = posy - 200;
	else
		var top = posy - 100;
	//alert("left " + left + " - top " + top);
	var num = Math.random();
	document.getElementById("emaildiv").style.visibility = "visible";
	document.getElementById("emaildiv").style.left = left + "px";
	document.getElementById("emaildiv").style.top = top + "px";
	thediv = "emaildiv";
	http.open('get', 'http://www.flamingoworld.com/demo/ajax/emailCouponToAFriend.php?couponid=' + couponid + "&rand=" + num);
	
	http.onreadystatechange = handleInfo;
	http.send(null);
}

function sendEmail(){
	//alert("send");
	var num = Math.random();
	var couponid = document.getElementById("couponid").value;
	var one = document.getElementById("one").value;
	var two = document.getElementById("two").value;
	var three = document.getElementById("three").value;
	var four = document.getElementById("four").value;
	var yourname = document.getElementById("yourname").value;
	var youremail = document.getElementById("youremail").value;
	
	thediv = "emaildiv";
	http.open('get', 'http://www.flamingoworld.com/demo/ajax/sendCouponToAFriend.php?couponid=' + couponid + "&name=" + escape(yourname) + "&email=" + escape(youremail) + "&one=" + escape(one) + "&two=" + escape(two) + "&three=" + escape(three) + "&four=" + escape(four) + "&rand=" + num);
	http.onreadystatechange = handleSendEmail;
	http.send(null);
	
}

function writeReview(merchantid,userid){
	var id = "top";
	var obj = document.getElementById(id);
	var posx = findPosX(obj);
	var posy = findPosY(obj);
	//alert("pos x " + posx + " - pos y " + posy);
	var left = posx + 60;
	var top = posy;
	
	var num = Math.random();
	document.getElementById("reviewdiv").style.visibility = "visible";
	document.getElementById("reviewdiv").style.left = left + "px";
	document.getElementById("reviewdiv").style.top = top + "px";
	thediv = "reviewdiv";
	http.open('get', 'http://www.flamingoworld.com/demo/ajax/writeReview.php?merchantid=' + merchantid + "&userid= " + userid + "&rand=" + num);
	
	http.onreadystatechange = handleInfo;
	http.send(null);
}

function handleInfo(){
	if(http.readyState == 4){
		var response = http.responseText;
		document.getElementById(thediv).innerHTML = response;
	}
}

function handleSendEmail(){
	if(http.readyState == 4){
		var response = http.responseText;
		document.getElementById("emaildiv").innerHTML = response;
		
		setTimeout(function() { closeEmail(); }, 2000);
	}
}

function closeEmail(){
	document.getElementById('emaildiv').style.visibility = "hidden";
}

function closeReview(){
	document.getElementById('reviewdiv').style.visibility = "hidden";
}

function hideTip(){
	document.getElementById(thediv).style.visibility = "hidden";
}

function getPageHeight(){
	var docHeight = (document.height !== undefined) ? document.height : document.body.offsetHeight;
	return docHeight;
}

function findPosX(obj){
	  var curleft = 0;
	  if(obj.offsetParent)
	      while(1) 
	      {
	        curleft += obj.offsetLeft;
	        if(!obj.offsetParent)
	          break;
	        obj = obj.offsetParent;
	      }
	  else if(obj.x)
	      curleft += obj.x;
	  return curleft;
	}

function findPosY(obj){
  var curtop = 0;
  if(obj.offsetParent)
      while(1)
      {
        curtop += obj.offsetTop;
        if(!obj.offsetParent)
          break;
        obj = obj.offsetParent;
      }
  else if(obj.y)
      curtop += obj.y;
  return curtop;
}

function clearSearch(){
	document.getElementById("query").value='';
}

function clearSignup(){
	document.getElementById("pemail").value='';
} 

function changeRating2(rating){
	alert("rating " + rating);
	document.getElementById("star1").src = 'http://www.flamingoworld.com/demo/images/star-empty.gif';
}

function changeRating(rating){
	var fullstar = 'http://www.flamingoworld.com/demo/images/star-full.gif';
	var emptystar = 'http://www.flamingoworld.com/demo/images/star-empty.gif';
	document.getElementById("rating").value = rating;
	
	//alert("RATING: " + rating);
	for(starnum = 1;starnum <= 5;starnum++){
		var idname = 'star' + starnum;
		//var fullstar = '<img src="http://www.flamingoworld.com/demo/images/star-full.jpg" border=0 id="star1" onMouseover="changeRating(' + stars + ');">';
		//var emptystar = '<img src="http://www.flamingoworld.com/demo/images/star-empty.jpg" border=0 id="star1" onMouseover="changeRating(' + stars + ');">';
		if(starnum <= rating){
			//document['star' + stars].src = fullstar;

			//document.getElementById("star" + starnum).src = fullstar;
			document.getElementById(idname).src = fullstar;
		}else{
			//document['star' + stars].src = emptystar;
			//document.getElementById("star" + starnum).src = emptystar;
			//alert("#" + starnum + " is empty");
			document.getElementById(idname).src = emptystar;
		}
	}
}

function submitReview(rating){
	var num = Math.random();
	var merchantid = document.getElementById("merchantid").value;
	var rating = document.getElementById("rating").value;
	var review = document.getElementById("review").value;
	var confirmit = document.getElementById("confirm").value;
	var userid = document.getElementById("userid").value;
	
	if(confirmit == 0){
		http.open('get', 'http://www.flamingoworld.com/demo/ajax/writeReview.php?merchantid=' + merchantid + "&rating=" + rating + "&review=" + escape(review) + "&userid=" + userid + "&rand=" + num);
	}else{
		http.open('get', 'http://www.flamingoworld.com/demo/ajax/saveReview.php?merchantid=' + merchantid + "&rating=" + rating + "&review=" + escape(review) + "&userid=" + userid + "&rand=" + num);
		closeReview();
		thediv = 'stars';
	}	
	http.onreadystatechange = handleInfo;
	http.send(null);
	
}

function verifyRegistration(){
	var dname = document.getElementById("name");
	var email = document.getElementById("email");
	var vemail = document.getElementById("vemail");
	var pword = document.getElementById("password");
	var vpword = document.getElementById("vpassword");
	
	var error = 0;
	
	if(dname.value.length == 0){
		alert("We would really like you to enter your Name. Otherwise we have to guess and we are terrible guesser.");
		dname.focus()
		error = 1;
		return false;
	}
	
	if(dname.value.length < 3 || dname.value.length > 32){
		alert("I'm sorry, your Name has to be between 3 and 32 (for people with really long names) characters long.");
		dname.focus()
		error = 1;
		return false;
	}
	
	if ((email.value == null) || (email.value=="")){
		alert("I think you forgot to enter your email address, we need that for your login name.");
		email.focus();
		error = 1;
		return false;
	}
	if (echeck(email.value) == false){
		alert("I'm sorry, we had touble verifying your email address.");
		email.value=""
		vemail.value=""
		email.focus()
		error = 1;
		return false;
	}
	
	if (email.value != vemail.value){
		alert("I'm sorry, you're email addresses don't seem to match.");
		email.focus()
		error = 1;
		return false;
	}
	
	if(pword.value.length < 4 || pword.value.length > 16){
		alert("I'm sorry, your Password has to be between 4 and 16 characters long.");
		pword.focus()
		error = 1;
		return false;
	}
	
	if(isAlphanumeric(pword) == false){
		alert("I'm sorry, your Password can only contains numbers and letters.");
		pword.focus()
		error = 1;
		return false;
	}
	
	if (pword.value != vpword.value){
		alert("I'm sorry, your passwords don't seem to match.");
		email.focus()
		error = 1;
		return false;
	}
	
	document.getElementById("mode").value = "register";
	document.aform.submit();
	
}

function submitForm(){
	document.aform.submit();
}

function submitFormatForm(){
	document.aform.submit();
}

function saveCoupon(couponid,userid,userkey){
	var num = Math.random();
	thediv = "messagediv";
	var id = "save" + couponid;
	var obj = document.getElementById(id);
	var posx = findPosX(obj);
	var posy = findPosY(obj);
	//alert("pos x " + posx + " - pos y " + posy);
	var height = getPageHeight();
	//alert("height " + height + " - posy: " + posy);
	var left = posx - 320;
	if(posy + 100 > height)
		var top = posy - 50;
	else
		var top = posy;
	document.getElementById("messagediv").style.visibility = "visible";
	document.getElementById("messagediv").style.left = left + "px";
	document.getElementById("messagediv").style.top = top + "px";
	http.open('get', 'http://www.flamingoworld.com/demo/ajax/saveCoupon.php?couponid=' + couponid + "&userid=" + userid + "&key=" + userkey + "&rand=" + num);
	http.onreadystatechange = handleSavedCoupon;
	http.send(null);
}

function promptSaveCoupon(couponid){
	var num = Math.random();
	thediv = "messagediv";
	var id = "save" + couponid;
	var obj = document.getElementById(id);
	var posx = findPosX(obj);
	var posy = findPosY(obj);
	//alert("pos x " + posx + " - pos y " + posy);
	var height = getPageHeight();
	//alert("height " + height + " - posy: " + posy);
	var left = posx - 320;
	if(posy + 100 > height)
		var top = posy - 50;
	else
		var top = posy;
	document.getElementById("messagediv").style.visibility = "visible";
	document.getElementById("messagediv").style.left = left + "px";
	document.getElementById("messagediv").style.top = top + "px";
	http.open('get', 'http://www.flamingoworld.com/demo/ajax/promptSaveCoupon.php?couponid=' + couponid + "&rand=" + num);
	http.onreadystatechange = handleSavedCoupon;
	http.send(null);
}

function handleSavedCoupon(){
	if(http.readyState == 4){
		
		var response = http.responseText;
		document.getElementById("messagediv").innerHTML = response;
	}
}

function checkNewPassword(){
	var pass1 = document.getElementById("password1");
	var pass2 = document.getElementById("password2");
	if(pass1.value.length < 4 || pass2.value.length > 16){
		alert("I'm sorry, your Password has to be at least 4 characters long.");
		document.getElementById("password1").value = "";
		document.getElementById("password2").value = "";
		document.getElementById("password1").focus();
		error = 1;
		return false;
	}
	
	if(pass1.value == pass2.value){
		document.bform.submit();
	}else{
		alert("Your new passwords do not match. Please enter them both again.");
		document.getElementById("password1").value = "";
		document.getElementById("password2").value = "";
		document.getElementById("password1").focus();
		return false;
	}
	
}

function checkNewEmail(){
	var email = document.getElementById("email");
	if ((email.value == null) || (email.value=="")){
		alert("I think you forgot to enter a new email address.");
		email.focus();
		error = 1;
		return false;
	}
	if (echeck(email.value) == false){
		alert("I'm sorry, we had touble verifying your new email address.");
		email.value=""
		email.focus()
		error = 1;
		return false;
	}
	document.bform.submit();
}

function verifyLogin(){
	var email = document.getElementById("lemail");
	var pword = document.getElementById("lpassword");
	
	if ((email.value == null) || (email.value=="")){
		alert("I think you forgot to enter your email address.");
		email.focus();
		error = 1;
		return false;
	}
	if (echeck(email.value) == false){
		alert("You didn't enter a valid email address.");
		email.value=""
		vemail.value=""
		email.focus()
		error = 1;
		return false;
	}
	
	document.getElementById("mode").value = "login";
	document.aform.submit();
}

function verifyContact(){
	var topic = document.getElementById("topic");
	var yname = document.getElementById("name");
	var email = document.getElementById("email");
	var subject = document.getElementById("subject");
	var message = document.getElementById("message");
	
	if ((topic.value == null) || (topic.value=="")){
		alert("So, why are you contacting us today?");
		email.focus();
		error = 1;
		return false;
	}
	
	if ((yname.value == null) || (yname.value=="")){
		alert("Could you please enter your name so we know who we are replying to.");
		yname.focus();
		error = 1;
		return false;
	}
	
	if ((email.value == null) || (email.value=="")){
		alert("You must enter a valid email address so we can reply to you.");
		email.focus();
		error = 1;
		return false;
	}
	
	if (echeck(email.value) == false){
		alert("You must enter a valid email address so we can reply to you.");
		email.value=""
		vemail.value=""
		email.focus()
		error = 1;
		return false;
	}
	
	if ((subject.value == null) || (subject.value=="")){
		alert("Could you please enter a subject for this message.");
		subject.focus();
		error = 1;
		return false;
	}
	
	if ((message.value == null) || (message.value=="")){
		alert("Did you realize you left the message blank? If you're trying to contact us you must have a reason.");
		message.focus();
		error = 1;
		return false;
	}
	
	document.getElementById("mode").value = "login";
	document.getElementById("s").value = "6918";
	document.getElementById("mode").value = "contact";
	document.aform.submit();
}

function isAlphanumeric(elem){
	var alphaExp = /^[0-9a-zA-Z]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		return false;
	}
}


function echeck(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   //alert("Invalid E-mail ID")
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   //alert("Invalid E-mail ID")
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    //alert("Invalid E-mail ID")
	    return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	    //alert("Invalid E-mail ID")
	    return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    //alert("Invalid E-mail ID")
	    return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	    //alert("Invalid E-mail ID")
	    return false
	 }
	
	 if (str.indexOf(" ")!=-1){
	    //alert("Invalid E-mail ID")
	    return false
	 }

	 return true					
}