/********************************************
Author: 
Date:
Description: This program provides many javascript functions
             used by other programs. This file includes several
             functions that allow programs on different
             levels in the application hierachy to call Calendar.aspx. 

Programs that call this file include: PriceListUpload.aspx,
                                      home_dashboard.aspx,
                                      and many others.

CHANGE LOG
----------
Programmer:   Bob Zuckerman 
Date:         1/11/2007 
Description:  Added function to call calendar from PriceListUpload.aspx
*********************************************/

var popUp; 

function OpenCalendar()//idname, postBack,left,top
{

	var idname=arguments[0];
	var postBack=arguments[1];
	var left=arguments[2];
	var top=arguments[3];
	var idbiz;
	if (arguments.length==5) {idbiz=arguments[4];}
	else {idbiz='';}
		//alert("Input " + idbiz);
	popUp = window.open('Calendar.aspx?formname=' + document.forms[0].name + 
		'&id=' + idname + '&selected=' + document.forms[0].elements[idname].value + '&postBack=' + postBack + '&idbiz=' + idbiz, 
		'popupcal', 
		'width=175,height=250,left=' + left + ',top=' + top);
}

function SetDate()//formName, id, newDate, postBack
{
    var formName=arguments[0];
	var id=arguments[1];
	var newDate=arguments[2];
	var postBack=arguments[3];
	var idbiz;
	eval('var theform = document.' + formName + ';');
	popUp.close();
	theform.elements[id].value = newDate;
	if (arguments.length==5)
		{ 
			idbiz=arguments[4];
			//alert(idbiz);
			theform.elements[idbiz].disabled = false;}

	if (postBack){
		//__doPostBack(id,'');
		}
}

function OpenCalendarLW()//idname, postBack,left,top
{

	var idname=arguments[0];
	var postBack=arguments[1];
	var left=arguments[2];
	var top=arguments[3];
	var idbiz;
	if (arguments.length==5) {idbiz=arguments[4];}
	else {idbiz='';}
		//alert("Input " + idbiz);
	popUp = window.open('../../Calendar.aspx?formname=' + document.forms[0].name + 
		'&id=' + idname + '&selected=' + document.forms[0].elements[idname].value + '&postBack=' + postBack + '&idbiz=' + idbiz, 
		'popupcal', 
		'width=175,height=250,left=' + left + ',top=' + top);
}

function openUsersList(idname,lidname,aid,postBack,left,top)
{  

	popUp = window.open('ActivityUsersList.aspx?formname=' + document.forms[0].name + 
		'&id=' + idname + '&lid=' + lidname + '&aid=' + aid +'&selected=' + document.forms[0].elements[idname].value + '&postBack=' + postBack, 
		'popupcal', 
		'width=740,height=560,left=' + left + ',top=' + top);
}

function SetUser(formName, id,lid,aid, LoginID,UserName, postBack)
{

	eval('var theform = document.' + formName + ';');	
	popUp.close();	
	theform.elements[id].value = UserName;
	theform.elements[lid].value = LoginID;
	theform.elements[aid].value = "Yes";
	if (postBack)
		__doPostBack(id,'');
}

function openContactList(idname,cidname,aid,comid,postBack,left,top)
{  

	popUp = window.open('ActivityContactList.aspx?formname=' + document.forms[0].name + 
		'&id=' + idname + '&cid=' + cidname + '&aid=' + aid +'&CompanyID=' + comid + '&selected=' + document.forms[0].elements[idname].value + '&postBack=' + postBack, 
		'popupcal', 
		'width=740,height=560,left=' + left + ',top=' + top);
}
function SetContact(formName, id, cid, aid, ContactID,FirstName,LastName, postBack)
{
	eval('var theform = document.' + formName + ';');
	popUp.close();	
	theform.elements[id].value = FirstName + ' ' + LastName ;
	theform.elements[cid].value = ContactID;	
	theform.elements[aid].value = "Yes";
	if (postBack)
		__doPostBack(id,'');
}

function openPartsList(formName,pidname,didname,aid,ClientName,postBack,left,top)
{  

	popUp = window.open('SPRPartsList.aspx?hformname=' + formName + 
		'&hpid=' + pidname + '&hdid=' + didname + '&haid=' + aid + '&hpostBack=' + postBack + '&Client=' + ClientName, 
		'popupcal', 
		'width=740,height=560,resizable=1,left=' + left + ',top=' + top);
}
function SetPart(formName, id,did,aid, PartNumber,PartDescription,Price, postBack)
{
    
//alert(formName);
//alert(id);
//alert(did);
//alert(aid);
//alert(PartNumber);
//alert(PartDescription);
//alert(Price);


	eval('var theform = document.' + formName + ';');	
	popUp.close();	
	theform.elements[id].value = PartNumber;
	theform.elements[did].value = PartDescription;
	theform.elements[aid].value = Price;
	//numFormat(theform.elements[aid],'$',',');
	//if (postBack)
		//__doPostBack(id,'');
}

function openFilter(formName,ftype,fname,cname,tname,rn,ClientName,postBack,left,top)
{
eval('var theform = document.' + formName + ';');
var fValue = theform.elements[fname].value ;
var cValue = theform.elements[cname].value ;

	popUp = window.open('FilterFields.aspx?hformname=' + formName + '&ftype=' + ftype +
		'&fname=' + fValue + '&cname=' + cValue + '&tname=' + tname + '&hpostBack=' + postBack + '&Client=' + ClientName, 
		'popupcal', 
		'width=500,height=560,left=' + left + ',top=' + top);
}

function openFilter1(formName,ftype,fname,cname,tname,rn,ClientName,postBack,left,top)
{

eval('var theform = document.' + formName + ';');
var fValue = theform.elements[fname].value ;
var cValue = theform.elements[cname].value ;

	popUp = window.open('FilterFields.aspx?hformname=' + formName + '&ftype=' + ftype +
		'&fname=' + fValue + '&cname=' + cValue + '&tname=' + tname + '&hpostBack=' + postBack + '&Client=' + ClientName, 
		'popupcal', 
		'width=500,height=560,left=' + left + ',top=' + top);
}

function SetFilter(formName, tname,FieldValue,postBack)
{   
  eval('var theform = document.' + formName + ';');	
  popUp.close();	
  theform.elements[tname].value = FieldValue;	
  //if (postBack)
 //__doPostBack(id,'');
 }
 
 function OpenFilterSet(ToUpdate)
{
var FilterToUpdate = ToUpdate ;
WinFeatures = "top=120,left=150,width=740,height=560,toolbar=no,menubar=no,scrollbars=no,titlebar=no"; 
window.open("FiltersSet.aspx?hFilterToUpdate="+FilterToUpdate,"",WinFeatures);
}

//This function will validate email_address
function search1(){
	var val1;
	var val2,val3;
	val2=0;
	val1= "@";
	var str1;
	val3= ThisPage.Email.value.length;
	str1 = ThisPage.Email.value;
	val2 = ThisPage.Email.value.search(val1);
	if (val2 == -1){
		alert("Invalid email address.");
		}
	else if(val2 == 0){
		alert("Invalid email address.");
		}
	else if((val2+1)== val3){
		alert("Invalid email address.");
		}
	else{
		mailcheck = true;
		}
   for (var i=0; i<val3-1; i++)
	{
	  if (str1.charAt(i)=="/" ){
	  alert("Invalid email address, / are not allowed");
	  mailcheck=false
	  }
	}
	for (var i=0; i<val3-2; i++)
	{  if (str1.charAt(i)==" "){
			alert("Please double check for errors in your email address.");
			mailcheck=false
		}
	}
   }
   
   function ckEmail(email){
    var primarymail;
        primarymail=true;
	var val1;
	var val2,val3;
	val2=0;
	 val1= "@";
	 var str1;
	if (email.value.length > 0){ 
	val3= email.value.length;
	str1 = email.value;
	val2 = email.value.search(val1);
	if (val2 == -1){
		alert("Invalid email address, the '@' is missing");
		select(email);
		return false;
		}
	else if(val2 == 0){
		alert("Invalid email address, the '@'is at the beginning");
		select(email)
		return false;
		}
	else if((val2+1)== val3){
		alert("Invalid email address, the '@'is at the end");
		select(email)
		return false;
		}
	else{
		primarymail = true;
		}
	for (var i=0; i<val3-1; i++)
	{
		if (str1.charAt(i)=="/" ){
			alert("Invalid Contact email address, / are not allowed");
			select(email)
			primarymail=false;
			return false;
			}
	}
	
	if(primarymail == true){
	return true;
	}
	}else
	return true;	
}

function select(field) {
	field.focus();
	field.select();
}

function ckPhoneArea(field,target)  {	
	var nextChar     = "";	
	var aString = field.value;
	for (var i=0; i < aString.length; i++)  {
		nextChar = aString.charAt(i);
		if (nextChar < "0" || nextChar > "9" ) {			
				alert("0 - 9 , please");
				field.value = "";
				i = aString.length;
				select(field);			
		}
	}
	
if (aString.length == 3){
	   	select(target);
	 }  	
}     // ckPhoneArea

function ckPhoneNumber(field)  {	
	var nextChar     = "";	
	var aString = field.value;
	for (var i=0; i < aString.length; i++)  {
		nextChar = aString.charAt(i);
		if (nextChar < "0" || nextChar > "9" ) {
			if (nextChar != "-" && nextChar != " " && nextChar != "x") {
				alert("0 - 9 , please");
				field.value = "";
				i = aString.length;
				select(field);
			}
		}
	}	
}     // ckPhoneNumber()

function ckPhoneExtension(field)  {	
	var nextChar     = "";	
	var aString = field.value;
	for (var i=0; i < aString.length; i++)  {
		nextChar = aString.charAt(i);
		if (nextChar < "0" || nextChar > "9" ){			
				alert("0 - 9 , please");
				field.value = "";
				i = aString.length;
				select(field);			
		}
	}	
}     // ckPhoneExtension()

function MM_openBrWindow(theURL,title,features) {
window.open(theURL,title,features);
}
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_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_findObj(n, d) { //v4.01
  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 && d.getElementById) x=d.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];}
}

function OpenCalendarForPriceListUpload()//idname, postBack,left,top
{

	var idname=arguments[0];
	var postBack=arguments[1];
	var left=arguments[2];
	var top=arguments[3];
	var idbiz;
	if (arguments.length==5) {idbiz=arguments[4];}
	else {idbiz='';}
		//alert("Input " + idbiz);
	popUp = window.open('Calendar.aspx?formname=' + document.forms[0].name + '&source=PriceListUpload' +
		'&id=' + idname + '&selected=' + document.forms[0].elements[idname].value + '&postBack=' + postBack + '&idbiz=' + idbiz, 
		'popupcal', 
		'width=175,height=250,left=' + left + ',top=' + top);
}

function CurrentYearWithAddress()
{
      var divcopyright = document.getElementById("divcopyright1")

      var d = new Date();
      var curr_year = d.getFullYear();


      tdcopy1.innerHTML = "&copy; "+ curr_year +" Computer Market Research, Ltd. All rights reserved. Mailing Address: 3545 Aero Court, Suite C | San Diego, CA 92123 | Phone: 858-279-6668."
}



