//  ********************************************************************
//  Provided by Barzi eCommerce - Maziar Barzi
//  ********************************************************************

//  Email Validation
RE=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/   // Email Validate
SP=/\S/												 	// Check Space 

//  ********************************************************************
//  ********************************************************************
function $(id) { 
	return document.getElementById(id);
 }
//  ********************************************************************
//  ********************************************************************
function Trim(STRING){
	STRING = LTrim(STRING);
	return RTrim(STRING);
}
/* *********************** */
function RTrim(STRING){
	while(STRING.charAt((STRING.length -1))==" "){
	STRING = STRING.substring(0,STRING.length-1);
	}
	return STRING;
}
/* *********************** */
function LTrim(STRING){
	while(STRING.charAt(0)==" "){
	STRING = STRING.replace(STRING.charAt(0),"");
	}
	return STRING;
}
/* *********************** */
function Left(STRING,CHARACTER_COUNT){
	return STRING.substring(0,CHARACTER_COUNT);
}
/* *********************** */
function Right(STRING,CHARACTER_COUNT){
	return STRING.substring((STRING.length - CHARACTER_COUNT),STRING.length);
}
/* *********************** */
function IsNumeric(VALUE){
	Noghte = 0
	if (VALUE.charCodeAt(0) == 46 || VALUE.charCodeAt(VALUE.length-1) == 46)
		return false;

	for(var b = 0; b < VALUE.length;b ++){
		if(VALUE.charCodeAt(b) == 46)
			Noghte = Noghte + 1
		if(VALUE.charCodeAt(b) < 48 || VALUE.charCodeAt(b) > 57)
			if(VALUE.charCodeAt(b) != 46)
				return false;
	}

	if (Noghte > 1)
		return false;

	return true;
}

//  ********************************************************************
//  ********************************************************************
//  Remove space from ant string
function removeSpaces(string) {
	var tstring = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)
	tstring += splitstring[i];
	return tstring;
}

//  ********************************************************************
//  ********************************************************************
//  Block key: . 1 2 3 4 5 6 7 8 9 0 and enter
function BlockKeyNum()
{
	// Numeric Code
	if (event.keyCode < 48 || event.keyCode > 57) 
		event.returnValue = false

	// . Character
	if (event.keyCode == 46)
		event.returnValue = true

	// Enter Code
	if (event.keyCode == 13 )
		event.returnValue = true		
}


//  ********************************************************************
//  ********************************************************************
//  Block key: . / 1 2 3 4 5 6 7 8 9 0 and enter
function BlockKeyNum2()
{
	// Numeric Code
	if (event.keyCode < 47 || event.keyCode > 57) 
		event.returnValue = false

	// . Character
	if (event.keyCode == 46)
		event.returnValue = true

	// Enter Code
	if (event.keyCode == 13 )
		event.returnValue = true		
}


//  ********************************************************************
//  ********************************************************************
//  Block key: 1 2 3 4 5 6 7 8 9 0 and enter
function BlockKeyNum3()
{
	// Numeric Code
	if (event.keyCode < 48 || event.keyCode > 57) 
		event.returnValue = false

	// Enter Code
	if (event.keyCode == 13 )
		event.returnValue = true		
}



//  ********************************************************************
//  ********************************************************************
//  Block key: a-z , A-Z and space
function BlockKeyNum4()
{
	// A-Z , a-z
	if ((event.keyCode < 65 || event.keyCode > 90) && (event.keyCode < 97 || event.keyCode > 122)) 
		event.returnValue = false

	// Space
	if (event.keyCode == 32 )
		event.returnValue = true		

	// Enter Code
	if (event.keyCode == 13 )
		event.returnValue = true		
}


//  ********************************************************************
//  ********************************************************************
//  Block key: a-z , A-Z , 1-9 , . / , and space
function BlockKeyNum5()
{
	// A-Z , a-z
		if ((event.keyCode < 65 || event.keyCode > 90) && (event.keyCode < 97 || event.keyCode > 122) && (event.keyCode < 44 || event.keyCode > 57)) 
		event.returnValue = false

	// &
	if (event.keyCode == 38 )
		event.returnValue = true		

	// Space
	if (event.keyCode == 32 )
		event.returnValue = true		

	// Enter Code
	if (event.keyCode == 13 )
		event.returnValue = true		
}


//  ********************************************************************
//  ********************************************************************
//  Comma , 3 digit
function Comma(inp) {
	inp = DeComma(inp)
	T = ""
	while(inp.length > 3){
		T = Right(inp, 3) + "," + T
		inp = Left(inp, inp.length - 3)
	}
	
	T = inp + "," + T
	return Left(T,T.length - 1)

}


//  ********************************************************************
//  ********************************************************************
//  DeComma , 3 digit
function DeComma(inp) {
	L = inp.length
	T = ""
	for(i=0; i<=L; i++)
		if (inp.substring(i,i+1) != ",")
			T = T + inp.substring(i,i+1)
	return T;
}

//  ********************************************************************
//  ********************************************************************
//  Preload Images
function FP_preloadImgs()
{
	var d=document,a=arguments; 
	if(!d.FP_imgs) d.FP_imgs=new Array();
	for(var i=0; i<a.length; i++)
	{ 
		d.FP_imgs[i]=new Image;
		d.FP_imgs[i].src=a[i];
	}
}




//  ********************************************************************
//  ********************************************************************
// If yek raghami bood ye sefr bezar jolosh
function IfZero(num) {
	return ((num <= 9) ? ("0" + num) : num);
}


//  ********************************************************************
//  ********************************************************************
// If yek raghami bood ye sefr bezar jolosh
function IfZeroOne(num) {
	return ((num.length == 1) ? ("0" + num) : num);
}




//  ********************************************************************
//  ********************************************************************
// Show Help TD
function iHelp() {
	if (document.getElementById("Help"))
	{
		if (document.getElementById("Help").className=="Close-TD NoPrint")
			document.getElementById("Help").className="Open-TD HelpBack"
		else
			document.getElementById("Help").className="Close-TD NoPrint"
	}
	else
	{
		window.status="     "
		window.setTimeout("window.status='';",3000)
	
	}
	return true
}





//  ********************************************************************
//  ********************************************************************
// MouseOver ID
function iHover(obj,classObj) {
	obj.className = classObj
}


//  ********************************************************************
//  ********************************************************************
// Change Textarea Height
function iChangeSize(obj,mode){
	if (mode==1 && parseInt(document.getElementById(obj).style.height) > 110 )
		document.getElementById(obj).style.height = parseInt(document.getElementById(obj).style.height) - 50 
	if (mode==2 && parseInt(document.getElementById(obj).style.height) < 410 )
		document.getElementById(obj).style.height = parseInt(document.getElementById(obj).style.height) + 50 
	if (mode==3)
		document.getElementById(obj).style.height = 110
}


//  ********************************************************************
//  ********************************************************************
// Change Font Size
function iFontSize(obj,mode){
	if (mode==1 && parseFloat(document.getElementById(obj).style.fontSize) > 8.5 ){
		tmp = parseFloat(document.getElementById(obj).style.fontSize) - 2
		tmp = tmp + "pt"
		document.getElementById(obj).style.fontSize = tmp
	}
	if (mode==2 && parseFloat(document.getElementById(obj).style.fontSize) < 14.5 ){
		tmp = parseFloat(document.getElementById(obj).style.fontSize) + 2
		tmp = tmp + "pt"
		document.getElementById(obj).style.fontSize = tmp
	}
	if (mode==3)
		document.getElementById(obj).style.fontSize = "8.5pt"
}



//  ********************************************************************
//  ********************************************************************
// HTML detection for <br>, <p>
function LineDetection(str){
	if (str == null)
		return false
		
	tool = str.length
	strout = ""
	
	for (i=0;i<=tool;i++){
		sel = str.substr(i,1)
		if (sel.charCodeAt(0) == 13)
			strout = strout + "<br> "
		else
			strout = strout + sel;
	}

	return strout;
}









//  ********************************************************************
//  ********************************************************************
// URL encode
var Url = {

	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},

	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}

//  ********************************************************************
//  ********************************************************************
// XML Object
function GetXmlHttpObject(){
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;
}



