
function reverse(field) {
	text = "";
	str = field.value = field.value;
	for (i = 0; i <= str.length; i++)
	text = str.substring(i, i+1) + text;
	field.value = field.value = text;
}

function checkVisa() {
	var tempDigit = document.form.cc_number.value;
	var tempLength = tempDigit.length;
	if((tempLength == 13) || (tempLength == 16)) {
		var visaTemp = tempDigit.substring(1,0);
		//alert(visaTemp);
		document.form.stripped.value = visaTemp;
		if(visaTemp != 4) {
			document.form.chking.value = "false";
			document.returnValue = false;
		}
		else {
			document.form.chking.value = "true";
			document.returnValue = true;
		}
	}
	else {
		document.form.chking.value = "false";
		document.returnValue = false;
	}
}

function checkMC() {
	var tempDigit = document.form.cc_number.value;
	var tempLength = tempDigit.length;
	if(tempLength == 16) {
		var mcTemp1 = tempDigit.substring(1,0);
		var mcTemp2 = tempDigit.substring(1,2);
		var mcTemp3 = mcTemp1 + mcTemp2;
		document.form.stripped.value = mcTemp3;
		if((eval(mcTemp3) < 51) || (eval(mcTemp3) > 55)) {
			document.form.chking.value = "false";
			document.returnValue = false;
		}
		else {
			document.form.chking.value = "true";
			document.returnValue = true;
		}
	}
	else {
		document.form.chking.value = "false";
		document.returnValue = false;
	}
}

function checkAMEX() {
	var tempDigit = document.form.cc_number.value;
	var tempLength = tempDigit.length;
	if(tempLength == 15) {
		var amexTemp1 = tempDigit.substring(1,0);
		var amexTemp2 = tempDigit.substring(1,2);
		var amexTemp3 = amexTemp1 + amexTemp2;
		//alert(amexTemp3);
		document.form.stripped.value = amexTemp3;
		if((eval(amexTemp3) < 34) || (eval(amexTemp3) > 37)) {
			document.form.chking.value = "false";
			document.returnValue = false;
		}
		else {
			document.form.chking.value = "true";
			document.returnValue = true;
		}
	}
	else {
		document.form.chking.value = "false";
		document.returnValue = false;
	}
}


function addEmUp() {
	var AddedUp = "";
	var tempStr = document.form.compile.value;
	for (i=0; i<tempStr.length; i=i+2) {
		temp = tempStr.substring(i, i+1);
		AddedUp = AddedUp + eval(temp * 1);
		temp = tempStr.substring(i+1, i+2);
		AddedUp = AddedUp + eval(temp * 2);
	}
	var temp2 = 0;
	for (z=0; z<AddedUp.length; z++) {
		temp2 = eval(temp2) + eval(AddedUp.substring(z, z+1));
	}
	document.form.total.value = AddedUp;
	document.form.total2.value = temp2;
	var temp20 = document.form.total2.value;
	
	var tempLength3 = temp20.length;
	var digit1 = eval(tempLength3);
	var digit2 = eval(tempLength3) - eval(1);
	var temp3 = temp20.substring(digit1,digit2);

	document.form.digit.value = temp3;
}

function validateCC() {
	//if(var tempCC = document.form.pay.value=="cc"){
	//alert("checken");
	var tempCC = document.form.cc_number.value;
	document.form.compile.value = tempCC;
	reverse(document.form.compile);
	addEmUp(document.form.compile);
	if(document.form.cc_type.selectedIndex == "2") {		
		checkVisa();		
	}
	if(document.form.cc_type.selectedIndex == "1") {
		checkMC();
	}
	if(document.form.cc_type.selectedIndex == "3") {
		checkAMEX();
	}
	//if(document.form.cc_type.selectedIndex == "3") {
		//checkDISC();
	//}
	var today = new Date();
	var year = today.getYear();
	//alert(year);
	var month = today.getMonth() + 1;
	//alert(month);
	if (year < 2000) year = 1900 + year;
	var dateMonth = document.form.cc_exp_month.selectedIndex + 1;
	//alert(dateMonth);
	var dateYear = document.form.cc_exp_year.selectedIndex + eval(year)-1;
	//alert(dateYear);
	var expired = "true";
	if(year >= dateYear) {
		if(month >= dateMonth) {
			expired = "true";
		}
		else {
			expired = "false";
		}
	}
	else {
		expired = "false";
	}
	var temp3 = document.form.digit.value;
	var testValid = document.form.chking.value;
	if(temp3 != 0) {
		alert("Credit Card is invalid!  Please, provide a valid credit card! error 0");
		document.form.cc_type.focus();
		return false;
	}
	if(testValid != "true"){
				 alert("Credit Card is invalid!  Please, provide a valid credit card! error 1");
		         document.form.cc_type.focus();
				 return false;
	}
	if(expired == "true") {
			   alert("Your credit card's expired!  Please, provide a valid credit card! error 2");
			   document.form.cc_exp_month.focus();
			   return false;
	}
		
	if((temp3 == 0) && (testValid == "true") && (expired == "false")) {
	  //  popup();
		
		
		return true;
	}
  //}
}
function popup(){
	/*
  var w = 480, h = 340;

  if (document.all || document.layers) {
     w = screen.availWidth;
     h = screen.availHeight;
  }

  var popW = 600, popH = 800;

  var leftPos = (w-popW)/2, topPos = (h-popH)/2;

  window.open('http://www.olympusfashionweek.com/press/thankyou.php','popup','width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos);
  */
}
