function resendCode(){
	var frmStr;
	frmStr = '<strong>Are you on Quammy?</strong><br/><form method="post" action="index.php?extSmsAuthFrm" name="mobile_number_form" id="mobile_number_form" onsubmit="return sbmForm(1)"><br/><p>Enter your mobile number to find out.</p><div id="mobile_number_err_desc" style="padding-top:10px;color:red"></div><table align="left" border="0" cellpadding="0" cellspacing="10" style="border:solid 0px #39598C"><tr class="add_email new_email tallrow"><td class="label">Mobile Number:</td><td><div class="email_option"><input type="text" size="2" maxlength="3" class="inputtext" value="" name="CountryCode" id="CountryCode" style="width:25px;text-align:center;">-<input type="text" class="inputtext" name="mobileNumber" id="mobileNumber"  style="width:100px;"></div></td></tr><tr><td></td><td><div class="buttons buttons_left"><input type="button" class="inputsubmit" id="save_email" name="SetNumber" onclick="sbmForm(1)" value="Get SMS Code" /></div></td></tr></table></form>';
	
	var popupdiv = document.getElementById('popup_content');						
	popupdiv.innerHTML = frmStr;
	
	sbmForm(4);
}

function removeProfile(ppkid){
   document.removep.hidppkid.value = ppkid;
   sbmForm(3);
}


function createCodeForm(){
	var frmStr;
	frmStr = '<strong>Confirmation code was sent to the number you specified.</strong><form method="post" action="index.php?extSmsAuthFrm" name="mobile_number_form" id="mobile_number_form" onsubmit="return sbmForm(2)"><table align="left" border="0" cellpadding="0" cellspacing="10" style="border:solid 0px #39598C"><tr class="add_email new_email tallrow"><td class="label" colspan="2"><div id="mobile_number_err_desc" style="color:red"></div></td></tr><tr class="add_email new_email tallrow"><td class="label">Enter the SMS code</td><td><div class="email_option"><input type="text" class="inputtext" name="AuthCode" id="AuthCode"></div></td></tr><tr><td></td><td><div class="buttons buttons_left"><input type="button" class="inputsubmit" id="save_email" name="SetNumber" onclick="sbmForm(2)" value="Submit Code" />&nbsp;&nbsp;<a href="javascript:resendCode()" style="font-size:15px;color:#0091E8">Get new code</a></div></td></tr></table></form>';
	
	var popupdiv = document.getElementById('popup_content');						
	popupdiv.innerHTML = frmStr;		
}

function checkNumeric(val){
    var telnoRegxp = /^([0-9]{1,1000})$/;
    if (!telnoRegxp.test(val)) {
        return false;
	}
	else{
	    return true;
	}
}

function validateEmail(address) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   //var address = document.getElementById(ElementId).value;
   if(reg.test(address) == false) {
        document.getElementById('emailErrMsg').innerHTML = "Please make sure your e-mail is typed correctly";
        return false;
   }
   else{
        return true;
   }
}


function sbmForm(frmType)
{
	//alert(frmType);
	var ajaxResponse,params,guiDiv,AjaxUrl;
	//AjaxUrl = "http://localhost/QuammyWebSite/QuammyWebSite/WebSite/ajaxRequests.php";
	AjaxUrl = "../ajaxRequests.php";
	guiDiv = document.getElementById("mobile_number_err_desc");
	
	exeAjax = false;
	
	if(frmType == 1){
		mobileNumber = document.getElementById('mobileNumber').value;
		CountryCode = document.getElementById('CountryCode').value;
		if(CountryCode != ""){
		    if(checkNumeric(CountryCode)){
		        if(validateNumber(true)) {
			        params = "?mobileNumber="+mobileNumber+"&CountryCode="+CountryCode+"&frmType=sms_ExtRemoveAuthFrm";
			        exeAjax = true;
		        }
		    }
		    else{
		        guiDiv.innerHTML = "You must enter a valid country code.";
		    }
		}
		else{
		    guiDiv.innerHTML = "Please enter your country code on the left.";
		}
		
	}
	else if(frmType == 2){
		AuthCode = document.getElementById('AuthCode').value;
		if(AuthCode != ""){
			params = "?AuthCode="+AuthCode+"&frmType=ExtRemoveAuthCodeFrm";
			exeAjax = true;
		}
		else{
			guiDiv.innerHTML = "Code was not entered";
		}
	}
	else if(frmType == 3){
	    hidppkid = document.getElementById('hidppkid').value;
	    params = "?hidppkid="+hidppkid+"&frmType=ExtRemoveProfileFrm";
	    exeAjax = true;
	}
	else if(frmType == 4 || frmType == 6){
	    if(document.getElementById('CountryCode').value == ""){
	        params = "?frmType=GetCountryByIP";
	        exeAjax = true;
	    }
	}
	else if(frmType == 5){
	    mobileNumber = document.getElementById('mobileNumber').value;
		CountryCode = document.getElementById('CountryCode').value;
		if(CountryCode != ""){
		    if(checkNumeric(CountryCode)){
		        if(validateNumber(true)) {
			        params = "?mobileNumber="+mobileNumber+"&CountryCode="+CountryCode+"&frmType=sms_downloadFrm";
			        exeAjax = true;
		        }
		    }
		    else{
		        guiDiv.innerHTML = "You must enter a valid country code.";
		    }
		}
		else{
		    guiDiv.innerHTML = "Please enter your country code on the left.";
		}
		
	}
	else if(frmType == 7){
	    UserEmail = document.getElementById('notified_email').value;
        if(validateEmail(UserEmail)){
	        params = "?notified_email="+UserEmail+"&frmType=NotifyEmailFrm";
	        //AjaxUrl = "http://localhost/QuammyWebSite/QuammyWebSite/WebSite/editaccount.php";
	        exeAjax = true;
	    }
	}
	else if(frmType == 8){
	    params = "?frmType=GetCountryByIP";
	    exeAjax = true;
	}
	
	if(exeAjax){
		outputResponse = true
		
		if (window.XMLHttpRequest)
		  {// code for IE7+, Firefox, Chrome, Opera, Safari
		  xmlhttp=new XMLHttpRequest();
		  }
		else
		  {// code for IE6, IE5
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		  
		xmlhttp.onreadystatechange=function()
		  {
		  if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				ajaxResponse = xmlhttp.responseText;
				if(frmType == 1){
					if(ajaxResponse == "Sent"){
						outputResponse = false;
						createCodeForm();
					}
				}
				else if(frmType == 2){
					if(ajaxResponse != "The SMS code was not authenticated"){
						guiDiv = document.getElementById('popup_content');
					}					
				}
				else if(frmType == 3){
					if(ajaxResponse == "1"){
					    guiDiv.innerHTML = "";
					    document.getElementById('ppkid'+hidppkid).innerHTML = "Your profile was removed successfully";
					    outputResponse = false;
					}
				}
				else if(frmType == 4){
					//if(ajaxResponse != ""){
					if(checkNumeric(ajaxResponse)){
					    document.getElementById('CountryCode').value = ajaxResponse;
					    outputResponse = false;
					}
				}
				else if(frmType == 5){
					if(ajaxResponse == "Sent"){
						outputResponse = false;
						guiDiv.innerHTML = "";
						document.getElementById("content_download_sms").style.display='none';
						document.getElementById('switchid').innerHTML = '<div class="content_part" id="content_dlcomplete_sms"><div id="downloadMenu" style="padding-left:30px"><a href="index.php" id="home"><i>sms</i></a></div><div id="downloadMenu" style="padding-left:235px"><a href="viapc.php" id="home"><i>pc</i></a></div><div id="downloadMenu" style="padding-left:440px"><a href="viawap.php" id="home"><i>wap</i></a></div><div id="download_sms"><br/>SMS message was sent to you! Open it and click the download link.<br/><br/>Once you have finished installing, Quammy will launch automatically.<br/><br/></div></div>';
						//document.getElementById('downloadSms').innerHTML = "SMS message was sent to you! Open it and click the download link.<br/><br/>Once you've finished installing, Quammy will launch automatically.<br/><br/>";
					}
				}
				else if(frmType == 6){
					if(checkNumeric(ajaxResponse)){
	                        document.getElementById("content_download_sms").style.display='block';
	                        document.getElementById("content_dlcomplete_sms").style.display='none';
	                        document.getElementById('CountryCode').value = ajaxResponse;
                    }
                    else{
                        document.getElementById("content_download_sms").style.display='none';
                        document.getElementById("content_dlcomplete_sms").style.display='block';
                    }
                    
					outputResponse = false;
				}
				else if(frmType == 7){
					document.getElementById("download_hide").innerHTML = "<br/><br/><br/><span style='font-size:22px;color:#0091E8'>"+ajaxResponse+"</span><br/><br/><br/>";
					
				}
				else if(frmType == 8){
					if(checkNumeric(ajaxResponse)){
                        document.getElementById("SignUpFrm").style.display='block';
                    }
					outputResponse = false;
				}
				
				if(outputResponse){
					guiDiv.innerHTML = ajaxResponse;
				}
			}
		  }
		xmlhttp.open("GET",AjaxUrl+params,true);
		xmlhttp.send();
	}
	

	return false;
}

    

//When you click on a link with class of poplight and the href starts with a # 
$(document).ready(function() {
	$('a.poplight[href^=#]').click(function() {
		var popID = $(this).attr('rel'); //Get Popup Name
		var popURL = $(this).attr('href'); //Get Popup href to define size

		//Pull Query & Variables from href URL
		var query= popURL.split('?');
		var dim= query[1].split('&');
		var popWidth = dim[0].split('=')[1]; //Gets the first query string value

		//Fade in the Popup and add close button
		$('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"><img src="images/close_pop.png" class="btn_close" border="0" title="Close Window" alt="Close" /></a>');

		//Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding  and border width defined in the css
		var popMargTop = ($('#' + popID).height() + 80) / 2;
		var popMargLeft = ($('#' + popID).width() + 80) / 2;

		//Apply Margin to Popup
		$('#' + popID).css({
			'margin-top' : -popMargTop,
			'margin-left' : -popMargLeft
		});

		//Fade in Background
		$('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
		$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies 
		
		resendCode();
		
		return false;
	});

	//Close Popups and Fade Layer
	$('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
		$('#fade , .popup_block').fadeOut(function() {
			$('#fade, a.close').remove();  //fade them both out
		});
		return false;
	});	
	
	
	
	//When you click on a link with class of popPresentation and the href starts with a # 
	$('a.popPresentation[href^=#]').click(function() {
		var popID = $(this).attr('rel'); //Get Popup Name
		var popURL = $(this).attr('href'); //Get Popup href to define size

		//Pull Query & Variables from href URL
		var query= popURL.split('?');
		var dim= query[1].split('&');
		var popWidth = dim[0].split('=')[1]; //Gets the first query string value

		//Fade in the Popup and add close button
		$('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"><img src="images/close_pop.png" class="btn_close" border="0" title="Close Window" alt="Close" /></a>');

		//Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding  and border width defined in the css
		var popMargTop = ($('#' + popID).height() + 80) / 2;
		var popMargLeft = ($('#' + popID).width() + 80) / 2;

		//Apply Margin to Popup
		$('#' + popID).css({
			'margin-top' : -popMargTop,
			'margin-left' : -popMargLeft
		});

		//Fade in Background
		$('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
		$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies 
		
		return false;
	});
	
	//Close Popups and Fade Layer
	$('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
		$('#fade , .pblock').fadeOut(function() {
			$('#fade, a.close').remove();  //fade them both out
		});
		return false;
	});	
});
