
function ValidatorTrim(s) {
    var m = s.match(/^\s*(\S+(\s+\S+)*)\s*$/);
    return (m == null) ? "" : m[1];
}

var dtCh= "/";
var minYear=01;
var maxYear=99;
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;
    }
    // All characters are numbers.
    return true;
}

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 stripCharsInBag(s, bag){
    var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checklength(obj,len)
{
    var temp = obj.value;
    if(temp.length < len)
        return false;
    return true;
}
function checkEmail_old(strng)
{
    var error = "";
    if (strng == "")
        {
        error = "You did not enter an email-address.\n";
        }
    var emailFilter=/^.+@.+\..{2,4}$/;
    if (!(emailFilter.test(strng)))
        {
        error = "Please enter a valid email address.\n";
        }
    var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
    if(strng.match(illegalChars))
        {
        error = "The email address contains illegal characters.\n";
        }
    return error;
}
function checkEmail(email)
{
  var error = "";
  if(email.length <= 0)
  {
    error = "You did not enter an email-address.\n";
    return error;
  }
  var splitted = email.match("^(.+)@(.+)$");
  if(splitted == null)
  {
      error = "Please enter a valid email address.\n";
      return error;
  }
  else
  {
    if(splitted[1] != null )
    {
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null)
      {
          error = "Please enter a valid email address.\n";
          return error;
      }
    }
    if(splitted[2] != null)
    {
      if((splitted[2]).indexOf(".")<=0)
      {
          error = "Please enter a valid email address.\n";
          return error;
      }
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null)
      {
          var regexp_ip =/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
          if(splitted[2].match(regexp_ip) == null)
          {
              error = "Please enter a valid email address.\n";
              return error;
          }
      }
      return error;
    }
  }
  error = "Please enter a valid email address.\n";
  return error;
}
function checkEmailX(strng,fieldcaption)
{
    var error = "";
    if (strng == "")
        {
        error = "You did not enter an "+fieldcaption+".\n";
        }
    var emailFilter=/^.+@.+\..{2,4}$/;
    if (!(emailFilter.test(strng)))
        {
        error = "Please enter a valid "+fieldcaption+".\n";
        }
    var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
    if(strng.match(illegalChars))
        {
        error = "The "+fieldcaption+" contains illegal characters.\n";
        }
    return error;
}

//Link Rollover starts here
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//link rollover ends here

//pre load images scrip starts here
var myimages=new Array()
function preloadimages()
{
    for (i=0;i<preloadimages.arguments.length;i++)
    {
        myimages[i]=new Image()
        myimages[i].src=preloadimages.arguments[i]
    }
}
function alwaysontop()
{
    window.focus();
}
function isInt(varvalue)
{
	var regexpINT = /\D/;
	return !regexpINT.test(varvalue);
}
function isIP(varvalue)
{
	var regexpIP = /^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;
	return regexpIP.test(varvalue);
}
function isSelected_Form_CheckBoxArray(formname,objname)
{
	var chkflg,strmessageid,url,returl;
	strmessageid="";
	chkflg=false;
	for(j=0; j<document.forms.length;j++)
	{
		if(document.forms[j].name==formname)
		{
			for (i=0;i<document.forms[j].elements.length;i++)
			{
				if (document.forms[j].elements[i].name==objname)
				{
					if (document.forms[j].elements[i].checked)
					{
						chkflg=true;
						break;
					}
				}
			}
		}
	}
	return chkflg;
}


/*******************************************************************************/
//added by chirag
//purpose: creates modaldialog for passed url
var ModalDialogWindow;
var ModalDialogInterval;
var ModalDialog = new Object;
ModalDialog.value = '';
ModalDialog.eventhandler = '';

function popupModalDialog(popupurl,width,height)
{
	if(typeof(width)=='undefined') width=500;
	if(typeof(height)=='undefined') height=500;

	if(window.showModalDialog)
	{
		window.showModalDialog(popupurl,window,'width='+width+',height='+height+',scrollbars=yes,location=no,status=no');
	}
	else
	{
		ModalDialogWindow=window.open(popupurl,window,'width='+width+',height='+height+',scrollbars=yes,location=no,status=no');
		ModalDialogInterval = window.setInterval("ModalDialogMaintainFocus()",1);
	}
}

function ModalDialogMaintainFocus()
{
	try
	{
		if (ModalDialogWindow.closed)
		{
			window.clearInterval(ModalDialogInterval);
			return;
		}
		ModalDialogWindow.focus();
	}
	catch (everything) {   }
}

function setWindowSize(w,h)
{
 if (parseInt(navigator.appVersion)>3)
 {
   if (navigator.appName=="Netscape")
   {
    top.outerWidth=w;
    top.outerHeight=h;
   }
   else top.resizeTo(w,h);
 }
}
function setActiveStyleSheet(title) {
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     if(a.getAttribute("rel").indexOf("style") != -1
        && a.getAttribute("title")) {
       a.disabled = true;
       if(a.getAttribute("title") == title) a.disabled = false;
     }
   }
}

function validateloginform()
{
	var err="";
	var focusfield="";
	var frm = document.loginForm;
	if(ValidatorTrim(frm.clsuser_email.value)=="")
	{
		err+="E-mail Address is required field.\n";
		if(focusfield=="") focusfield = "clsuser_email";
	}
	if(ValidatorTrim(frm.clsuser_password.value)=="")
	{
		err+="Password is required field.\n";
		if(focusfield=="") focusfield = "clsuser_password";
	}
	
	if(err!="")
	{
		alert(err);
		setFocus(focusfield);
		return false;
	}
	else
	{
		frm.clsuser_hdnsubmited.value = 1;
		frm.clsuser_hdnaction.value = "LI";
	}
}

function setFocus(focusfield)
{
     document.getElementById(focusfield).focus();
}

function clearfield(varfield)
{
	if((varfield.name == 'clsuser_email' && varfield.value == 'E-mail Address') || (varfield.name == 'clsuser_password' && varfield.value == 'password'))
	{
		varfield.value = '';
	}
	
}



//shopping cart functions
function addtocart(product_id,price)
{
	cookiename = "rmcart";
	totalcookiename = "rmcarttotal";
	cookievalue =get_cookie ( cookiename );
	totalcookievalue =get_cookie ( totalcookiename );
	if(cookievalue == null)
	{
		cookievalue = product_id+"@@"+price;
		totalcookievalue = "1"+"@@"+price;
	}
	else
	{
		arrproducts = cookievalue.split("###");
		blnproductexist = false;
		for(i=0; i<arrproducts.length; i++)
		{
			
			arrdetail = arrproducts[i].split("@@");
			if(arrdetail[0] == product_id)
			{
				blnproductexist = true;
				break;
			}
		}
		
		if(!blnproductexist)
		{
			cookievalue = cookievalue + "###" + product_id + "@@" + price;
			
			totalcookiedet = totalcookievalue.split("@@");
			
			totalprod = parseInt(totalcookiedet[0])+1;
			totalprice = parseInt(totalcookiedet[1])+parseInt(price);
			totalcookievalue  = totalprod + "@@" + totalprice;
		}
	}
	

	expiredays = 1;
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	
	//products selected
	document.cookie=cookiename+ "=" +escape(cookievalue)+
	((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
	
	//total amount cookie
	document.cookie=totalcookiename+ "=" +escape(totalcookievalue)+
	((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}


function removefromcart(arrproducts)
{
	//alert(arrproducts);
	
	cookiename = "rmcart";
	totalcookiename = "rmcarttotal";
	cookievalue =get_cookie ( cookiename );
	//totalcookievalue =get_cookie ( totalcookiename );
	if(cookievalue == null)
	{
		cookievalue = null;
		totalcookievalue = null;
	}
	else
	{
		arrcookie_products = cookievalue.split("###");
		cookievalue = "";
		totalprod = "0";
		totalprice = "0";
		for(i=0; i<arrcookie_products.length; i++)
		{	
			arrdetail = arrcookie_products[i].split("@@");
			
			blnproduct = false;
			for(var j=0; j<arrproducts.length; j++) 
			{
				if (arrproducts[j] == arrdetail[0])
				{
					blnproduct = true;
					break;
				}
			}

			
			if(!blnproduct)
			{
				cookievalue = cookievalue + "###" + arrdetail[0] + "@@" + arrdetail[1];
				totalprod = parseInt(totalprod)+1;
				totalprice = parseInt(totalprice)+parseInt(arrdetail[1]);
				
			}
		}
	}
	
	totalcookievalue = totalprod+"@@"+totalprice;
	//alert(totalcookievalue);
	//alert(cookievalue);
	cookievalue = trim(cookievalue, "###");
	//alert(cookievalue);
	//return false;
	if(totalprod == "0")
	{
		//products selected
		document.cookie=cookiename+ "=" +escape(cookievalue)+
		";expires=Thu, 01-Jan-1970 00:00:01 GMT";
		
		//total amount cookie
		document.cookie=totalcookiename+ "=" +escape(totalcookievalue)+
		";expires=Thu, 01-Jan-1970 00:00:01 GMT";
	

	}
	else
	{
		expiredays = 1;
		var exdate=new Date();
		exdate.setDate(exdate.getDate()+expiredays);
		
		//products selected
		document.cookie=cookiename+ "=" +escape(cookievalue)+
		((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
		
		//total amount cookie
		document.cookie=totalcookiename+ "=" +escape(totalcookievalue)+
		((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
	
	}
}

//get existing cookie value
function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}


function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

var formnwsignup_submitted = false;
function checknwsignupfields(frmname)
{
	var err = '';
	var strFocusField =	'';
	var validemail = '';
	var frm = eval('document.'+frmname);

	if(ValidatorTrim(frm.clsnewsletter_email.value)=="")
	{
		err += "Email address is required field. \n";
		if(strFocusField=='')
		strFocusField	=	"clsnewsletter_email";
	}
	else
	{
		validemail = checkEmail(ValidatorTrim(frm.clsnewsletter_email.value));

		if(validemail!="")
		{
			err += checkEmail(frm.clsnewsletter_email.value);
			strFocusField	=	"clsnewsletter_email";
		}
	}
	
	if(err!="")
	{
		alert(err);
		
		setFocus(strFocusField);
		return false;
	}

	if(formnwsignup_submitted)
	{
		return false;
	}
	else
	{
		formnwsignup_submitted = true;
		frm.clsnewsletter_hdnaction.value = "SaveNW";
		frm.clsnewsletter_hdnsubmited.value = "1";
		return true;
	}
}
