//Jul 1, 2008 - K. Chau - Absolute Path


//change select menu for Province/State
function changeMenu(){
	//clear province select menu
	provlen = document.frmOnlineCarShow.Province.options.length;
	for (i=0; i<provlen-1; i++){
		document.frmOnlineCarShow.Province.options[1] = null;
	}

	sidx = document.frmOnlineCarShow.Country.selectedIndex;

	//other
	if (sidx == 3){
		document.frmOnlineCarShow.Province.options[1] = new Option("Other","Other");
	}
	else{
		//set values of province select menu from array
		for (i=0; i<arrProv[sidx].length; i++) {
			document.frmOnlineCarShow.Province.options[i+1] = new Option(arrProv[sidx][i], arrProv[sidx][i]);
		}
	}

	document.frmOnlineCarShow.Province.options[0].selected = true;
}

//change select menu for Province/State
function changeSignupMenu(){
	//clear province select menu
	provlen = document.frmOnlineCarShow.sProvince.options.length;
	for (i=0; i<provlen-1; i++){
		document.frmOnlineCarShow.sProvince.options[1] = null;
	}

	sidx = document.frmOnlineCarShow.sCountry.selectedIndex;

	//other
	document.frmOnlineCarShow.sProvince.options[0].selected = true;
	if (sidx == 3){
		document.frmOnlineCarShow.sProvince.options[1] = new Option("Other","Other");
		document.frmOnlineCarShow.sProvince.options[1].selected = true;
	}
	else{
		//set values of province select menu from array
		for (i=0; i<arrProv[sidx].length; i++) {
			document.frmOnlineCarShow.sProvince.options[i+1] = new Option(arrProv[sidx][i], arrProv[sidx][i]);
			if (arguments[0] == arrProv[sidx][i]) {
				document.frmOnlineCarShow.sProvince.options[i+1].selected = true;
			}
		}
	}
}

//change select menu for Category
function changeCategory(){
	//clear sub category select menu
	var sublen = document.frmOnlineCarShow.SubCategory.options.length;
	for (i=0; i<sublen-1; i++){
		document.frmOnlineCarShow.SubCategory.options[1] = null;
	}
	
	i = document.frmOnlineCarShow.Category.selectedIndex-1;
	if (i<0) return;

	document.frmOnlineCarShow.SubCategory.options[0].selected = true;
	//set values of sub categories select menu from array
	for (j=0; j<arrSub[i].length; j++) {
		document.frmOnlineCarShow.SubCategory.options[j+1] = new Option(arrSub[i][j][2], arrSub[i][j][1]);
		if (arguments[0] == arrSub[i][j][1]) {
			document.frmOnlineCarShow.SubCategory.options[j+1].selected = true;
		}
	}
}

function changeSignupCategory(){
	//clear sub category select menu
	var sublen = document.frmOnlineCarShow.sSubCategory.options.length;
	for (i=0; i<sublen-1; i++){
		document.frmOnlineCarShow.sSubCategory.options[1] = null;
	}

	i = document.frmOnlineCarShow.sCategory.selectedIndex-1;
	if (i<0) return;

	//set values of sub categories select menu from array
	document.frmOnlineCarShow.sSubCategory.options[0].selected = true;
	for (j=0; j<arrSub[i].length; j++) {
		document.frmOnlineCarShow.sSubCategory.options[j+1] = new Option(arrSub[i][j][2], arrSub[i][j][1]);
		if (arguments[0] == arrSub[i][j][1]) {
			document.frmOnlineCarShow.sSubCategory.options[j+1].selected = true;
		}
	}
}

///change select menu for Models
function changeModel(){
	//clear model select menu
	var modellen = document.frmOnlineCarShow.Model.options.length;
	for (i=0; i<modellen-1; i++){
		document.frmOnlineCarShow.Model.options[1] = null;
	}

	i = document.frmOnlineCarShow.Make.selectedIndex-1;
	if (i<0) return;

	//set values of models select menu from array
	for (j=0; j<arrModel[i].length; j++) {
		document.frmOnlineCarShow.Model.options[j+1] = new Option(arrModel[i][j][2], arrModel[i][j][1]);
	}

	document.frmOnlineCarShow.Model.options[0].selected = true;
}

function changeSignupModel(){
	//clear model select menu
	var modellen = document.frmOnlineCarShow.sModel.options.length;
	for (i=0; i<modellen-1; i++){
		document.frmOnlineCarShow.sModel.options[1] = null;
	}

	i = document.frmOnlineCarShow.sMake.selectedIndex-1;
	if (i<0) return;

	//set values of models select menu from array
	document.frmOnlineCarShow.sModel.options[0].selected = true;
	for (j=0; j<arrModel[i].length; j++) {
		document.frmOnlineCarShow.sModel.options[j+1] = new Option(arrModel[i][j][2], arrModel[i][j][1]);

		if (arguments[0] == arrModel[i][j][1]) {
			document.frmOnlineCarShow.sModel.options[j+1].selected = true;
		}
	}

	
}


function submitPage(){
	//alert(arguments[0]);
	
	document.frmOnlineCarShow.page.value = arguments[0];
	document.frmOnlineCarShow.task.value = arguments[1];
	document.frmOnlineCarShow.subtask.value = arguments[2];
	document.frmOnlineCarShow.id1.value = arguments[3];
	document.frmOnlineCarShow.id2.value = arguments[4];
	document.frmOnlineCarShow.id3.value = arguments[5];
	document.frmOnlineCarShow.id4.value = arguments[6];
	document.frmOnlineCarShow.id5.value = arguments[7];
	document.frmOnlineCarShow.id6.value = arguments[8];
	
	if ( arguments[0].match('http://') )
		document.frmOnlineCarShow.action=arguments[0] + '.cfm';
	else
		document.frmOnlineCarShow.action='/' + arguments[0] + '.cfm';
	
	document.frmOnlineCarShow.submit();
}

function imgChange(imgName,fileName){
	if (document.images){
		document[imgName].src=fileName;
	}
}

function carImgChange(imgName,fileName,descr){
	if ( fileName != '' ) {
		//alert(fileName);
		imgChange(imgName,fileName);
		
		if (arguments[4] != 'undefined')
			document.getElementById('carPhoto_link').href = arguments[4];
		else
			document.getElementById('carPhoto_link').href = fileName.replace('m_','l_');
	}
		
	if ( descr != '' && document.getElementById('car_photo_descr')) {
		document.getElementById('car_photo_descr').innerHTML = descr.replace('\\','');
	}
	
	if (arguments[3] != 'undefined' && arguments[3] > 0)
		currentCarImageIndex = arguments[3];
}

currentCarImageIndex = 1;
maxCarImageIndex = 5;
function carImgSwitch(imgName,i){
	maxCarImageIndex = document.frmOnlineCarShow.maxCarImageIndex.value;
	var tempIndex = parseInt(currentCarImageIndex) + parseInt(i);
	if ( tempIndex > 0 && tempIndex <= maxCarImageIndex ) {
		var tempImgName = eval('document.frmOnlineCarShow.carImagePath'+tempIndex);
		var tempImgDescr = eval('document.frmOnlineCarShow.carImageDescr'+tempIndex);
		if ( tempImgDescr.value != '' ) {
			carImgChange(imgName,tempImgName.value,tempImgDescr.value);
			currentCarImageIndex = tempIndex;
			
			if ( currentCarImageIndex <= 1 ) {
				document.getElementById('car_photo_prev').style.display='none';
				document.getElementById('car_photo_next').style.display='block';
			} else if ( currentCarImageIndex >= maxCarImageIndex ) {
				document.getElementById('car_photo_prev').style.display='block';
				document.getElementById('car_photo_next').style.display='none';
			} else {
				document.getElementById('car_photo_prev').style.display='block';
				document.getElementById('car_photo_next').style.display='block';
			}
				
				
		} else {
			carImgSwitch(imgName,parseInt(i)+1);
		}
	}
}


function enLarge(){
	window.open('enlargephoto.cfm?name=' + document.carPhoto.src.replace('m_','l_') + '&id=' + arguments[0],'largePhoto','width=430,height=400');
}

function CheckKeyIE(e)
{
	if (navigator.appName == "Netscape")
    	keyPressed = String.fromCharCode(e.which);
 	else
        keyPressed = String.fromCharCode(window.event.keyCode);

 	if (keyPressed == "\r" || keyPressed=="\n")
  	{
		// NOTE - the next line is causing errors on MacIE - need to trim the whitespace from the arguments?
		submitPage(arguments[0].replace(/ /, ""),arguments[1].replace(/ /,""),arguments[2].replace(/ /,""),arguments[3],arguments[4],arguments[5],arguments[6],arguments[7],arguments[8]);
  	}
}

function CheckKeyIELogin(e)
{
	if (navigator.appName == "Netscape")
    	keyPressed = e.keyCode? e.keyCode : e.charCode
 	else
        keyPressed = window.event.keyCode;

 	if (keyPressed == 13)
  	{
		// NOTE - the next line is causing errors on MacIE - need to trim the whitespace from the arguments?
		ajaxLogin();
  	}
}


function isValidDate(strDate) {
     // (\d{1,2}) means 4 or 12
     // (\/|-) means either (/ or -), 4-12 or 4/12 
     // NOTE: we have to escape / (\/)
     // or else pattern matching will interpret it to mean the end instead of the literal "/"
     // \2 use the 2nd placeholder (\/|-) "here"
     // (\d{2}|\d{4}) means 02 or 2002
     var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
     var matchArray = strDate.match(datePat);

     if (matchArray == null) return false;

     // matchArray[0] will be the original entire string, for example, 4-12-02 or 4/12/2002
     var month = matchArray[1];     // (\d{1,2}) - 1st parenthesis set - 4
     var day = matchArray[3];         // (\d{1,2}) - 3rd parenthesis set - 12
     var year = matchArray[4];        // (\d{2}|\d{4}) - 5th parenthesis set - 02 or 2002

     if (month < 1 || month > 12) return false;
     if (day < 1 || day > 31) return false;
     if ((month == 4 || month == 6 || month==9 || month == 11) && day == 31) return false;
     if (month == 2) {
          var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));

          if (day > 29 || (day == 29 && !isleap)) return false;
     }
     return true;
}
