// Site functions
var lastImage=1;

//******************************
// Product page
//******************************
function setPrText(txt){
	document.getElementById('prtext').innerHTML=txt;
}
function handleMore(){
	document.getElementById('pr3w').style.display='none';
	if(document.getElementById('pr3').style.display==''){
		document.getElementById('pr3').style.display='none';
		document.getElementById('prclose').style.display='none';
	}else{
		document.getElementById('pr3').style.display='';
		document.getElementById('prclose').style.display='';
	};
}
function handleWhere(){
	document.getElementById('pr3').style.display='none';
	if(document.getElementById('pr3w').style.display==''){
		document.getElementById('pr3w').style.display='none';
		document.getElementById('prclose').style.display='none';
	}else{
		document.getElementById('pr3w').style.display='';
		document.getElementById('prclose').style.display='';
	};
}
function handleClose(){
	document.getElementById('pr3').style.display='none';
	document.getElementById('pr3w').style.display='none';
	document.getElementById('prclose').style.display='none';
}

function switchImage(image){
	document.getElementById("img"+lastImage).src="images/icon.image.gif";
	document.getElementById("img"+image).src="images/icon.image.selected.gif";
	lastImage=image;
	document.getElementById("content900600").style.backgroundImage="url('IMAGES/PRODUCTS/"+Right(document.getElementById("himg"+image).src, 12)+"')";
}
function setLink(fld,lnk){
	if(document.selection){
		fld.focus();
		var sel=document.selection.createRange();
		if(lnk.value!=''){
			sel.text='<A HREF=\''+lnk.value+'\' TARGET=\'new\'>'+sel.text+'</A>';
		}
	}else{
		if(fld.selectionStart!=fld.selectionEnd){
			if(lnk.value!=''){
				fld.value=fld.value.substring (0,fld.selectionStart)+'<A HREF=\''+lnk.value+'\' TARGET=\'new\'>'+fld.value.substring(fld.selectionStart, fld.selectionEnd)+'</A>'+fld.value.substring(fld.selectionEnd);
			}
		}
	}
}
function linkToProductOrderForm(fld){
	if(document.selection){
		fld.focus();
		var sel=document.selection.createRange();
		sel.text='<A HREF=\'?a=order&c='+document.getElementById('txtGUID').value+'\'>'+sel.text+'</A>';
	}else{
		if(fld.selectionStart!=fld.selectionEnd){
			fld.value=fld.value.substring (0,fld.selectionStart)+'<A HREF=\'?a=order&c='+document.getElementById('txtGUID').value+'\'>'+fld.value.substring(fld.selectionStart, fld.selectionEnd)+'</A>'+fld.value.substring(fld.selectionEnd);
		}
	}
}
function popUpPayment(linkId){
	var popUpRadioWindow = window.open("payment.asp?LinkId="+linkId,"payment","scrollbars=no,left=10,top=10,screenX=10,screenY=10,width=620,height=600");
}
//******************************
// Contact
//******************************
function validContact(){
	var msg='';

	if(document.getElementById('enquiry').value=='')msg=msg+'Please enter inquiry.\n';
	if(document.getElementById('contactname').value=='')msg=msg+'Please enter your name.\n';
	if(!testemail(document.getElementById('contactemail').value))msg=msg+'Please enter valid email address.\n';

	if(msg!=''){
		alert(msg);
		return false;
	}
	return true;
}
function validEmail(){
	var msg='';

	if(!testemail(document.getElementById('email').value))msg=msg+'Please enter valid email address.\n';

	if(msg!=''){
		alert(msg);
		return false;
	}
	return true;
}
//******************************
// Orders
//******************************
function selectComponent(componentId, selectedComponent){
	document.getElementById('component'+componentId).value=selectedComponent;
}
function handleCheckBox(c,h){
	if(c.checked){h.value='1'}else{h.value='0'};
}
function validOrder(){
	var msg='';

	if(document.getElementById('CustomizeIt').value=='1'&&document.getElementById('Customization').value=='')msg=msg+'Please describe desired customization.\n';
	if(document.getElementById('Trade').value=='1'&&document.getElementById('ResaleNumber').value=='')msg=msg+'Please enter Resale Number.\n';
	if(document.getElementById('ContactName').value=='')msg=msg+'Please enter your name.\n';
	if(!testemail(document.getElementById('ContactEmail').value))msg=msg+'Please enter valid email address.\n';
	if(document.getElementById('ContactPhone').value=='')msg=msg+'Please enter a phone number.\n';

	if(msg!=''){
		alert(msg);
		return false;
	}
	return true;
}
//******************************
// Admin formatting
//******************************
function setBold(fld){
	if(document.selection){
		fld.focus();
		var sel=document.selection.createRange();
		sel.text='<B>'+sel.text+'</B>';
	}else{
		if(fld.selectionStart!=fld.selectionEnd){
			fld.value=fld.value.substring (0,fld.selectionStart)+'<B>'+fld.value.substring(fld.selectionStart, fld.selectionEnd)+'</B>'+fld.value.substring(fld.selectionEnd);
		}
	}
}
function setItalic(fld){
	if(document.selection){
		fld.focus();
		var sel=document.selection.createRange();
		sel.text='<I>'+sel.text+'</I>';
	}else{
		if(fld.selectionStart!=fld.selectionEnd){
			fld.value=fld.value.substring (0,fld.selectionStart)+'<I>'+fld.value.substring(fld.selectionStart, fld.selectionEnd)+'</I>'+fld.value.substring(fld.selectionEnd);
		}
	}
}
//******************************
// Misc
//******************************
function isIE(){
	//alert(navigator.appVersion);
	if(navigator.appVersion.indexOf("MSIE") != -1)return true;
    return(false);
}
function testemail(src) {
	var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
	return regex.test(src);
}
function printWindow() {
	bV = parseInt(navigator.appVersion);
	if (bV >= 4) window.print();
}
//******************************
// Trim
//******************************
function LTrim(str){
	var i = 0;
	var j = str.length - 1;

	if(str==null)return ('');

	for (i = 0; i < str.length; i++){
		if(str.substr(i, 1)!=' '&&str.substr(i, 1)!='\t')break;
	}

	if(i<=j)
		return (str.substr(i, (j + 1) - i));
	else
		return ('');
}
function RTrim(str){
	var i = 0;
	var j = str.length - 1;

	if(str==null)return ('');

	for(j = str.length - 1; j >= 0; j--){
		if(str.substr(j, 1)!=' '&&str.substr(j, 1)!='\t')break;
	}

	if (i<=j)
		return (str.substr(i, (j + 1) - i));
	else
		return ('');
}
function Trim(str){
	if(str==null)return ('');
	return RTrim(LTrim(str));
}
//******************************
// Left, Right
//******************************
function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}
//******************************
// Date validation
//******************************
var dtCh= "/";

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    return true;
}
function stripCharsInBag(s, bag){
	var i;
    var returnString = "";

    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}
function daysInFebruary (year){
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}
function isDate(dtStr){
	var daysInMonth = DaysArray(12);
	var pos1 = dtStr.indexOf(dtCh);
	var pos2 = dtStr.indexOf(dtCh, pos1 + 1);
	var strDay = dtStr.substring(0, pos1);
	var strMonth = dtStr.substring(pos1 + 1, pos2);
	var strYear = dtStr.substring(pos2 + 1);

	strYr = strYear;

	if (strDay.charAt(0) == "0" && strDay.length > 1) strDay = strDay.substring(1);
	if (strMonth.charAt(0) == "0" && strMonth.length > 1) strMonth = strMonth.substring(1);
	if (strYr.charAt(0) == "0" && strYr.length > 3) strYr = strYr.substring(1);

	month = parseInt(strMonth);
	day = parseInt(strDay);
	year = parseInt(strYr);

	if (pos1 == -1 || pos2 == -1){
		//alert("Date format should be: dd/mm/yyyy.");
		return false;
	}
	if (strMonth.length < 1 || month < 1 || month > 12){
		//alert("Please enter valid month.");
		return false;
	}
	if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter valid day.");
		return false;
	}
	if (strYear.length != 4 || year == 0){
		//alert("Please enter 4 digit year.");
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1) != -1 || isInteger(stripCharsInBag(dtStr, dtCh)) == false){
		//alert("Please enter valid date.");
		return false;
	}
	return true;
}
//******************************
// Encoding
//******************************
function urlEncode(str){
	str = escape(str);
	return str.replace(/[*+\/@]|%20/g,
		function(s){
			switch(s){
				case "*": s = "%2A"; break;
				case "+": s = "%2B"; break;
				case "/": s = "%2F"; break;
				case "@": s = "%40"; break;
				case "%20": s = "+"; break;
			}
			return s;
		}
	);
}
