
//Global vars to hold connection to web pages
var xmlHttp;
var xmlHttp2;


function GetXmlHttpObject() {
	//Determine what browser we are on and make a httprequest connection for ajax
	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;
}


// ------- ajax_showprofile -----------

function ajax_showProfile(mc_id){ 
   xmlHttp = GetXmlHttpObject();
    if (xmlHttp != null) {
		
		document.getElementById('sp_loading').style.display = '';
		document.getElementById('spotlight_profile').style.display = 'none';
		
		xmlHttp.onreadystatechange=res_showProfile;
		
        xmlHttp.open("GET", "includes/cis_ajax/show_profile.php?mc_id="+mc_id, true);
		xmlHttp.send(null); 

    }
}

function res_showProfile() { 
	//IF this is getting called when the page is done loading the states then output the div
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
	 	//Update the Div tag with the outputted text
		document.getElementById('sp_loading').style.display = 'none';
		document.getElementById('spotlight_profile').style.display = '';
	 	document.getElementById("spotlight_profile").innerHTML=xmlHttp.responseText;
	} 
}

// ------- add product to cart -----------

function add2cart(id){ 
	xmlHttp = GetXmlHttpObject();
    if (xmlHttp != null) {
		document.getElementById('cart_loading').style.display = '';
		document.getElementById('buynow_btn').style.display = 'none';
		xmlHttp.onreadystatechange=output_add2cart;
        xmlHttp.open("GET", "includes/cis_ajax/add2cart.php?p_id="+id, true);
		xmlHttp.send(null); 

    }
}

function output_add2cart(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
	 	//Update the Div tag with the outputted text
		document.getElementById('cart_loading').style.display = 'none';
		document.getElementById('buynow_btn').style.display = '';
		document.getElementById('cart_total').innerHTML=xmlHttp.responseText;
	 	$("#viewcart").trigger('click');
	} 

}

// ------- add product to cart -----------  


// ------- add Count to cart -----------

function count_cart(){ 
	xmlHttp = GetXmlHttpObject();
    if (xmlHttp != null) {
		xmlHttp.onreadystatechange=output_count_cart;
        xmlHttp.open("GET", "includes/cis_ajax/count_cart.php", true);
		xmlHttp.send(null); 

    }
}

function output_count_cart(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		document.getElementById("cart_total").innerHTML=xmlHttp.responseText;
	}
}



// ------- update product to cart -----------

function update_cart(id,qty){ 
	xmlHttp = GetXmlHttpObject();
    if (xmlHttp != null) {
		xmlHttp.onreadystatechange=output_update_cart;
        xmlHttp.open("GET", "includes/cis_ajax/update_cart.php?p_id="+id+"&qty="+qty, true);
		xmlHttp.send(null); 

    }
}

function output_update_cart(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		//document.getElementById("cart_total").innerHTML=xmlHttp.responseText;
		show_cart_items();
		//count_cart2();
		//$("#viewcart").trigger('click');
	}
 
}

// ------- update product to cart -----------  


// ------- update product to cart -----------

function show_cart_items(){ 
	xmlHttp = GetXmlHttpObject();
    if (xmlHttp != null) {
		xmlHttp.onreadystatechange=output_show_cart_items;
        xmlHttp.open("GET", "includes/cis_ajax/cart_items.php", true);
		xmlHttp.send(null); 

    }
}

function output_show_cart_items(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		document.getElementById("cart_table").innerHTML=xmlHttp.responseText;
	}
 
}

// ------- update product to cart ----------- 


// ------- LOGIN -----------

function ajax_login(id,pw){ 
	xmlHttp = GetXmlHttpObject();
    if (xmlHttp != null) {
		document.getElementById("login_loding").style.display = '';
		document.getElementById("login_result").style.display = 'none';
		xmlHttp.onreadystatechange=res_ajax_login;
        xmlHttp.open("GET", "includes/cis_ajax/ajaxlogin.php?id="+id+"&pw="+pw, true);
		xmlHttp.send(null); 

    }
}

function res_ajax_login(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		var result = xmlHttp.responseText;
		result = result.length;
		//alert(result);
		if(result<=5)
		{
			document.location='login.php';
			//javascript:history.go(-1);
		}else{
		
		document.getElementById("login_loding").style.display = 'none';
		document.getElementById("login_result").style.display = '';
		//document.getElementById("login_result").innerHTML = httpObject.responseText;
		}
	}
 
}

// ------- LOGIN ----------- 

// ------- FORGET PASSWORD -----------

function ajax_forget_pw(id){  
	xmlHttp = GetXmlHttpObject();
    if (xmlHttp != null) {
		document.getElementById("forgot_loding").style.display = '';
		document.getElementById("forgot_result").style.display = 'none';
		document.getElementById("forgot_mailed").style.display = 'none';
		xmlHttp.onreadystatechange=res_ajax_forget_pw;
        xmlHttp.open("GET", "includes/cis_ajax/ajaxforget_pw.php?id="+id, true);
		xmlHttp.send(null); 

    }
}

function res_ajax_forget_pw(){
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		var result = xmlHttp.responseText;
		result = result.length;
		if(result <= 2)
		{
			//document.location='index.php';
			//javascript:history.go(-1);
			document.getElementById("forgot_loding").style.display = 'none';
			document.getElementById("forgot_result").style.display = 'none';
			document.getElementById("forgot_mailed").style.display = '';
			//document.getElementById("login_result").innerHTML = httpObject.responseText;
			
		}else{
		
		document.getElementById("forgot_loding").style.display = 'none';
		document.getElementById("forgot_result").style.display = '';
		document.getElementById("forgot_mailed").style.display = 'none';
		//document.getElementById("login_result").innerHTML = httpObject.responseText;
		
		}
	}
}

// ------- FORGET PASSWORD ----------- 


// ------- verification code -----------

function ajax_send_vcode(nos){ 
	xmlHttp = GetXmlHttpObject();
    if (xmlHttp != null) {
		document.getElementById("sms_loding").style.display = '';
		xmlHttp.onreadystatechange=output_send_vcode;
        xmlHttp.open("GET", "includes/cis_ajax/send_vcode.php?nos="+nos, true);
		xmlHttp.send(null); 

    }
}

function output_send_vcode(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		document.getElementById("sms_loding").style.display = 'none';
		document.getElementById("send_vcode").style.display = 'none';
		document.getElementById("get_vcode").style.display = '';
		var result = xmlHttp.responseText;
		result = result.replace(/(\r\n|\n|\r)/gm,"");
		document.getElementById("sms_sent_to").value=result;
		document.getElementById("txt_sent_to").innerHTML="+91"+result;
	}
 
}


function ajax_verify_code(vcode,nos){ 
	xmlHttp = GetXmlHttpObject();
    if (xmlHttp != null) {
		document.getElementById("code_loding").style.display = '';
		xmlHttp.onreadystatechange=output_verify_code;
        xmlHttp.open("GET", "includes/cis_ajax/verify_code.php?nos="+nos+"&vcode="+vcode, true);
		xmlHttp.send(null); 

    }
}

function output_verify_code(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		document.getElementById("code_loding").style.display = 'none';
		var result = xmlHttp.responseText;
		result = result.replace(/(\r\n|\n|\r)/gm,"");
		res_len = result.length;
		if(res_len<=3)
		{
			document.thisForm.action = "updateme.php";		

			document.thisForm.submit();
			
		}else{
			alert(result);
			return false;
		}
	}
 
}

// ------- Verification code ----------- 


// ------------ other common function for this project


function clearField(Field){
document.getElementById(Field).value="";	
}

// ------- Trim Function ----------- 
function Trim(str)

{

    while (str.substring(0,1) == ' ') // check for white spaces from beginning
    {
        str = str.substring(1, str.length);
    }
    while (str.substring(str.length-1, str.length) == ' ') // check white space from end
    {
        str = str.substring(0,str.length-1);
    }
  
    return str;
} 
// ------- Trim Function ----------- 



