// Funtions common

/**
*	Ajax functions
*
*/
function ajaxPost(url, idResult, arrResult, callback){
	$.post(url, {
   		
	 },
	function(data){		
		if(typeof(idResult) != "undefined")
		if(idResult)
			document.getElementById(idResult).innerHTML	= data;
		if(typeof(arrResult) != "undefined")
		if(arrResult.length > 0){
			var i=0;
			for(i=0;i<arrResult.length;i++)	
				document.getElementById(arrResult[i]).innerHTML	= data;
		}
		if(callback)
			eval(callback);
	});

}

function jsonPost(url, idResult, arrResult, callback){
	$.post(url, {
   		
	 },
	function(data){			
		
		if(data.error){			
			alert(data.error);
			return false;
		}
		
		if(data.success == 'addToCart'){	
			//alert(data.message);
			Tip('<blink>'+data.message+'</blink>',FONTCOLOR, '#ff0000',BGCOLOR, '#FDFF00');
			if(typeof(idResult) != "undefined"){				
				ShowCart(idResult);				
			}
		}
		if(data.success == 'deleteFromCart'){
			if(typeof(idResult) != "undefined"){				
				ShowCart(idResult);				
			}
		}
		if(typeof(idResult) != "undefined"){				
			if(idResult && data.message !='undefined' && typeof(data.message) != "undefined"){
				document.getElementById(idResult).innerHTML	= data.message;
			}
		}
		if(callback)
			eval(callback);
		
	},'json');

}

function goToUrl(gotourl){
	if(gotourl)
		window.location.href=gotourl;
}

function popUp(URL, param) {
	day = new Date();
	id = day.getTime();
	if(typeof(param) != "undefined")
		eval("page" + id + " = window.open(URL, '" + id + "', '"+param+"');");
	else
		eval("page" + id + " = window.open(URL, '" + id + "', 'scrollbars=0,location=0,statusbar=1,menubar=0,resizable=1,width=800,height=400,left = 290,top = 85');");
}		

function addToCart(pd_id, color, size, idResult){	
	if(typeof(idResult) == "undefined")
		idResult	= 'id_result_cart';
	if(typeof(color) == "undefined")
		color	= '';
	if(typeof(size) == "undefined")
		size	= '';
	if(!pd_id){
		alert('Please choose product');
		return false;
	}
	else{
		var url	= "index.php?page=ajax&action=addToCart&pd_id="+pd_id+"&color="+color+"&size="+size+"&idResult="+idResult;
		jsonPost(url,idResult);
	}
}



function verify_quantity(item){
	if(isNaN(item.value)){
		undo(item);
		//item.value = 0;
	}	
	if(Number(item.value) > 500){
		item.value = 500;
	}	
}



function ShowCart(idResult){
	if(typeof(idResult) == "undefined")
		idResult	= 'id_result_cart';
	var url	= "index.php?page=ajax&action=ShowCart&idResult="+idResult;
	jsonPost(url,idResult);
}


function deleteFromCart(pd_id,color,idResult,arrResult,callback){
	if(typeof(idResult) == "undefined")
		idResult	= 'id_result_cart';
	if(!pd_id){
		alert('Please choose product');
		return false;
	}
	else{
		var url	= "index.php?page=ajax&action=deleteFromCart&pd_id="+pd_id+"&color="+color+"&idResult="+idResult;
		jsonPost(url,idResult,arrResult,callback);
	}
}

function deleteAllCart(){
	if(typeof(idResult) == "undefined")
		idResult	= 'id_result_cart';

	var url	= "index.php?page=ajax&action=deleteAllCart&idResult="+idResult;
	jsonPost(url,idResult);
}

function loadDay(code_city_get,code_day_get,idResult){
	
	var url	= "index.php?page=ajax&action=Loadday&code_city_get="+code_city_get+"&code_day_get="+code_day_get+"&idResult="+idResult;
	
	jsonPost(url,idResult);
	
	document.getElementById('id_result_cart_post_time').innerHTML	= '';	
}

function loadTime(code_city_get,code_day_get,code_time_get,idResult){
	
	var url	= "index.php?page=ajax&action=Loadtime&code_city_get="+code_city_get+"&code_day_get="+code_day_get+"&code_time_get="+code_time_get+"&idResult="+idResult;
	
	jsonPost(url,idResult);
}
function IsNumeric(sText){
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++){ 
	  Char = sText.charAt(i); 
	  if (ValidChars.indexOf(Char) == -1){
		 IsNumber = false;
		 }
	  }
   return IsNumber;
}


function CreateBookmarkLink() {
	if(document.all)
	window.external.AddFavorite(location.href,document.title);
	else if(window.sidebar)window.sidebar.addPanel
	(document.title,location.href,'');
}
function show(id){	
	var o = document.getElementById(id);	
	if (o.style.visibility =='hidden' || o.style.visibility ==''){
		o.style.visibility='visible';
	}
	else{
		o.style.visibility='hidden';
	}
}
function hidden(id){
	var o = document.getElementById(id);	
	o.style.visibility ='hidden';
}

<!--
/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Turnea Iulian :: http://www.eurografic.ro */

function iObject() {
  this.i;
  return this;
}

var myObject=new iObject();
myObject.i=0;
var myObject2=new iObject();
myObject2.i=0;
store_text=new Array();

//store_text[0] store initial textarea value
store_text[0]="";

function countclik(tag) {
  myObject.i++;
  var y=myObject.i;
  var x=tag.value;
  store_text[y]=x;
}

function undo(tag) {
  if ((myObject2.i)<(myObject.i)) {
    myObject2.i++;
  } else {
    //alert("Finish Undo Action");
  }
  var z=store_text.length;
  z=z-myObject2.i;
  if (store_text[z]) {
  	tag.value=store_text[z];
  } else {
  	tag.value=store_text[0];
  }
}

function redo(tag) {
  if((myObject2.i)>1) {
    myObject2.i--;
  } else {
   // alert("Finish Redo Action");
  }
  var z=store_text.length;
  z=z-myObject2.i;
  if (store_text[z]) {
    tag.value=store_text[z];
  } else {
  tag.value=store_text[0];
  }
}

function setInteger(objValue) {

	var varTEST = /^([0-9]\,{0,})*$/;

	var varTESTNumOnly = /^[0-9]*$/;

	var strCommaSplit;

	var strFormatted;

	var intCount = 0;

	var intBalance = 0;

	var blnValid = true;

	var strArray;

	//var i = window.event.keyCode;

	

	//if ( (i==37) || (i==39) ) {

	//}

	//else

	//{

		if ( objValue.value != "" ) {

			if ( varTEST.test( objValue.value ) ) {

				strCommaSplit = objValue.value.split(",");

				

				strFormatted = "";

				if ( strCommaSplit.length > 1 ) {

					for ( intCount = 0; intCount < strCommaSplit.length; intCount++ ) {

						strFormatted += strCommaSplit[intCount];

					}

				}else{

					strFormatted = objValue.value;

				}

				

				while ( blnValid == true ) {

					intBalance = strFormatted.length;

					blnValid = false;

					

					if ( intBalance > 1 ){

						if (strFormatted.substring(0,1) == "0"){

							strFormatted = strFormatted.substring(1);

						}								

					}			

				}

				

				intCount = 0;

				strArray = new Array(5);

				

				while ( intBalance > 3 ) {

					strArray[intCount] = strFormatted.substring(strFormatted.length - 3, strFormatted.length);

					strFormatted = strFormatted.substring(0, strFormatted.length - 3);

					intBalance = strFormatted.length;

					intCount++;

				}

				

				for ( intCount = strArray.length; intCount >= 0; intCount-- ) {

					if ( varTESTNumOnly.test( strArray[intCount] ) ) {

						strFormatted += "," + strArray[intCount];

					}

				}

				

				objValue.value = strFormatted;

					

				

			}

			else {

				strFormatted = objValue.value;

				objValue.value = strFormatted.substring(0, strFormatted.length - 1);

				setInteger(objValue);

			}

		}

	//}

}



function setIntegerNoZero(objValue) {

	var varTEST = /^([0-9]\,{0,})*$/;

	var varTESTNumOnly = /^[0-9]*$/;

	var strCommaSplit;

	var strFormatted;

	var intCount = 0;

	var intBalance = 0;

	var blnValid = true;

	var strArray;

	//var i = window.event.keyCode;

	

	//if ( (i==37) || (i==39) ) {

	//}

	//else

	//{

		if ( objValue.value != "" ) {

			if ( varTEST.test( objValue.value ) ) {

				strCommaSplit = objValue.value.split(",");

				

				strFormatted = "";

				if ( strCommaSplit.length > 1 ) {

					for ( intCount = 0; intCount < strCommaSplit.length; intCount++ ) {

						strFormatted += strCommaSplit[intCount];

					}

				}else{

					strFormatted = objValue.value;

				}

				

				while ( blnValid == true ) {

					intBalance = strFormatted.length;

					blnValid = false;

					

					

				}

				

				intCount = 0;

				strArray = new Array(5);

				

				objValue.value = strFormatted;

					

				

			}

			else {

				strFormatted = objValue.value;

				objValue.value = strFormatted.substring(0, strFormatted.length - 1);

				setIntegerNoZero(objValue);

			}

		}

	//}

}


